javascript 从包含特定值的数组中获取对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19501371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get an object from array which contains a specific value
提问by Neil
I am trying to search through an array of objects using Underscore.js, but I can't seem to target the one I want.
我正在尝试使用 Underscore.js 搜索一组对象,但我似乎无法定位到我想要的对象。
console.log(_.findWhere(response.data, { TaskCategory: { TaskCategoryId: $routeParams.TaskCategory } }));
However, this is returning undefined
$routeParams.TaskCategory
is equal to 301
然而,这是返回undefined
$routeParams.TaskCategory
等于301
This is an example of the objects inside the array I am searching. This data is represented by data.response
这是我正在搜索的数组内的对象的示例。该数据表示为data.response
[{
"TaskCategory": {
"TaskCategoryId": 201,
"TaskName": "TaskName"
},
"TaskCount": 1,
"Tasks": [{
"EventTypeId": 201,
"EventName": "Event Driver",
"EventDate": "0001-01-01T00:00:00",
"EventId": "00000000-0000-0000-0000-000000000000",
}]
},
{
"TaskCategory": {
"TaskCategoryId": 301,
"TaskName": "TaskName"
},
"TaskCount": 1,
"Tasks": [{
"EventTypeId": 201,
"EventName": "Event Driver",
"EventDate": "0001-01-01T00:00:00",
"EventId": "00000000-0000-0000-0000-000000000000",
}]
}]
So I want the second object in that array using the TaskCategory.TaskCategoryId
, is it possible to get it using Underscore?
所以我想要该数组中的第二个对象使用TaskCategory.TaskCategoryId
,是否可以使用 Underscore 获取它?
回答by McGarnagle
Use _.find
instead of findWhere:
使用_.find
代替 findWhere:
console.log(_.find(response.data, function(item) {
return item.TaskCategory.TaskCategoryId == $routeParams.TaskCategory;
}));
They are similar, but findWhere
is designed for special cases where you want to match key-value pairs (not useful in your scenario as it involves nested objects). Find
is more general-use, because it lets you provide a function as the predicate.
它们很相似,但findWhere
专为您想要匹配键值对的特殊情况而设计(在您的场景中没有用,因为它涉及嵌套对象)。 Find
更通用,因为它允许您提供一个函数作为谓词。
回答by forivall
The source of _.findWhere
/ _.where
is as follows
_.findWhere
/的来源_.where
如下
_.where = function(obj, attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key]) return false;
}
return true;
});
};
_.findWhere = function(obj, attrs) {
return _.where(obj, attrs, true);
};
As you can see, it performs strict equality rather than deep equality. If you want a deep searching where, this would suffice (untested, unoptimized):
如您所见,它执行严格相等而不是深度相等。如果你想深入搜索哪里,这就足够了(未经测试,未经优化):
_.whereDeep = function(obj, attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key] || !(_.isObject(attrs[key]) && _.whereDeep([value[key]], attrs[key], true))) return false;
}
return true;
});
};
_.findWhereDeep = function(obj, attrs) {
return _.whereDeep(obj, attrs, true);
};
And you would be able to use your code, almost unchanged
你将能够使用你的代码,几乎没有改变
_.findWhereDeep(response.data, { TaskCategory: { TaskCategoryId: $routeParams.TaskCategory } });