'How to separate an item that contains in a list?
List a =[{"Label":"item 1","Value":"10"},{"Label":"item","Value":"20"},{"Label":"item 3","Value":"30"}];
List b =[{"Label":"item 1","Value":"10"},{"Label":"item C","Value":"12"},{"Label":"item D","Value":"15"}];
List c = [];
print(c);
//expected [{"Label":"item 1","Value":"10"}]
Solution 1:[1]
Assuming lists has the same length and ordering:
List a =[{"Label":"item 1","Value":"10"},{"Label":"item","Value":"20"},{"Label":"item 3","Value":"30"}];
List b =[{"Label":"item 1","Value":"10"},{"Label":"item C","Value":"12"},{"Label":"item D","Value":"15"}];
List c = [];
for (var i = 0; i < a.length; i++) {
if(a[i]['Label'] == b[i]['Label'] && a[i]['Value'] == b[i]['Value']){
c.add(a[i]);
}
}
print(c);
//[{"Label":"item 1","Value":"10"}]
Solution 2:[2]
I don't think this is an optimal solution but this should work in this case:
List c = a.where((element) => b.map((e) => e.toString()).contains(element.toString())).toList();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ivo Beckers |
