Confusing title, the only simple way to explain is to show you what I'm after :
var user = [
{foo:"test",bar:1},
{foo:"test2",bar:2}
];
var items = [{foo:"test",bar:1},{foo:"test4",bar:4},{foo:"test5",bar:5}]
What I want, is to pick one item from items
which is not already in user
, and add it to user
. In this case the user
object would end up looking like :
user = [
{foo:"test",bar:1},
{foo:"test2",bar:2},
{foo:"test4",bar:4}
];
I've tried all sorts of _.filter, _.contains, etc... combinations, but can't quite figure it out. Any help would be very appreciated!
The logic is as follows: you want to find the first item that is not contained within the user
array.
So you can do the following:
var item = _.find(items, function (item) {
return !_.findWhere(user, item);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.