1 | initial version |
You can use the where
method on a list of JSON data to filter based on a condition specified in another set of data. Here is an example:
import 'dart:convert';
void main() {
// JSON data to filter
final jsonData = '[{"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "Bob", "age": 20}]';
// Condition to filter by
final agesToKeep = [20, 25];
// Convert JSON data to a list of maps
final List<Map<String, dynamic>> data = List<Map<String, dynamic>>.from(json.decode(jsonData));
// Filter the data based on the agesToKeep condition
final filteredData = data.where((person) => agesToKeep.contains(person['age'])).toList();
print(filteredData); // [{"name": "John", "age": 25}, {"name": "Bob", "age": 20}]
}
In this example, we convert the JSON data to a list of maps using json.decode
. Then, we use the where
method to filter the data based on the agesToKeep
condition. Finally, we convert the filtered data back to a list using toList
.