[英]Lodash/Underscore : Check for a value within an array, in an array of objects
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
. 我想要的是从user
尚不存在的items
选择一项 ,然后将其添加到user
。 In this case the user
object would end up looking like : 在这种情况下, user
对象最终看起来像:
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. 我已经尝试过各种_.filter,_。contains等...组合,但是还不太清楚。 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. 逻辑如下:您要查找user
数组中未包含的第一项。
So you can do the following: 因此,您可以执行以下操作:
var item = _.find(items, function (item) {
return !_.findWhere(user, item);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.