I have 2 maps and I want to get the missing elements as a list. For example:
var map1= [
{"name":"name1","email":"[email protected]"},
{"name":"name2","email":"[email protected]"},
{"name":"name3","email":"[email protected]"},
];
var map2= [
{"name":"name1","email":"[email protected]"},
{"name":"name2","email":"[email protected]"},
];
Output: [{"name":"name3","email":"[email protected]"}]
I tried this approach:
var removedElements = map2.where((element) =>
!map1
.contains(element['email']))
.toList();
but it doesn't work. Any help would be great.
CodePudding user response:
try with this
void main() {
var map1 = [
{"name": "name1", "email": "[email protected]"},
{"name": "name2", "email": "[email protected]"},
{"name": "name3", "email": "[email protected]"},
];
var map2 = [
{"name": "name1", "email": "[email protected]"},
{"name": "name2", "email": "[email protected]"},
];
var removedElements = [];
var k;
for (var i in map2) {
for (var j in map1) {
if (i["email"] != j["email"]) {
k = j;
}
}
removedElements.add(k);
}
print(removedElements.toSet().toList());
}
Another approach
var removedElements =
map2.where((element) {
for(var i in map1){
if(i["email"] == element["email"]){
return false;
} else{
return true;
}
}
}).toList();
print(removedElements);
output : [{name: name3, email: [email protected]}]