Home > OS >  How to separate an item that contains in a list?
How to separate an item that contains in a list?

Time:04-26

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"}]

CodePudding user response:

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"}];

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"}]

CodePudding user response:

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();
  • Related