'PHP array_diff() equivalent in Dart? (Comparing two maps and return a map of difference)
I simply want to produce the same effect that PHP's array_diff()
function does in Dart,
and for those who don't know PHP or this function, my apologies but unfortunately don't know how to describe my problem better than using PHP, but in short lines, it simply takes two arrays, compares them and returns an array with any difference for example:
$post = ["name" => "First Name", "views" => 30];
$postUpdated = ["name" => "Second Name", "views" => 30];
$result = array_diff($postUpdated,$post);
printing $result
gives:
Array
(
[name] => Second Name
)
Which is exactly what I want but in flutter (Dart), for instance if I have:
var post = {"name": "First Name", "views": 30};
var postUpdated = {"name": "Second Name", "views": 30};
how can I get the changed values, I mean to get a variable that is equal to:
{"name": "Second Name"}
Now sure I can make a function that compares my two maps explicitly but I want a general solution that can take any two maps and returns a map of difference. Thanks.
Solution 1:[1]
I know this post is 6 months old, but it was the first hit when I had the same question. One option (avoiding the obvious for-in loop solution) that works is to use Map.removeWhere:
var post = {"name": "First Name", "views": 30};
var postUpdated = {"name": "Second Name", "views": 30};
postUpdated.removeWhere((k,v) => v == post[k]);
print (postUpdated); // {name: Second Name}
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 |