过滤条件下划线
Underscore where filter
我正在尝试使用 Underscore 的 where
来过滤对象数组,但我似乎无法弄清楚如何让它为同一个键过滤多个值。例如:
var itemsArr = [{name:"foo", color:"red"}, {name:"bar", color:"blue"}, {name:"bob", color:"yellow"}];
我正在尝试将其添加到 return 一个包含所有颜色为 red
的项目以及所有颜色为 blue
的项目的数组...这可能吗?我试过了
tempArr = _.where(itemsArr, {color:["red", "blue"]});
但这没有用。如果我必须只使用 _.filter
并写出我自己的谓词,我会的,但我只是想知道是否有其他人尝试过这样做并找到了解决方案。
根据 documentation:
Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.
如果您愿意,也可以查看 source code。在幕后,where
使用 isMatch
:
_.isMatch = function(object, attrs) {
var keys = _.keys(attrs), length = keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};
因为你不能有两个相同的钥匙,所以你不能做你想做的事。我会按照你的建议使用 filter
。
你能 运行 _.where 两次并连接两个结果数组吗?
tempArr = _.where(itemsArr, {color: "red"}).concat(_.where(itemsArr, {color: "blue"}));
这是解决方案
var itemsArr = [{name:"foo", color:"red"}, {name:"bar", color:"blue"}, {name:"bob", color:"red"}];
var filter = ["red","blue"];
var data = {};
_.each(filter, function (item) {
data[item] = true;
});
var returnData = _.filter(itemsArr, function (item) {
return data[item.color];
});
console.log(returnData)
使用filter函数:
_.filter(itemsArr,function(itm){return itm.color=='red' || itm.color=='blue'})
_.filter(itemsArr, function(item, index) {
return _.contains(["red", "blue"], item.color);
})
我正在尝试使用 Underscore 的 where
来过滤对象数组,但我似乎无法弄清楚如何让它为同一个键过滤多个值。例如:
var itemsArr = [{name:"foo", color:"red"}, {name:"bar", color:"blue"}, {name:"bob", color:"yellow"}];
我正在尝试将其添加到 return 一个包含所有颜色为 red
的项目以及所有颜色为 blue
的项目的数组...这可能吗?我试过了
tempArr = _.where(itemsArr, {color:["red", "blue"]});
但这没有用。如果我必须只使用 _.filter
并写出我自己的谓词,我会的,但我只是想知道是否有其他人尝试过这样做并找到了解决方案。
根据 documentation:
Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.
如果您愿意,也可以查看 source code。在幕后,where
使用 isMatch
:
_.isMatch = function(object, attrs) {
var keys = _.keys(attrs), length = keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};
因为你不能有两个相同的钥匙,所以你不能做你想做的事。我会按照你的建议使用 filter
。
你能 运行 _.where 两次并连接两个结果数组吗?
tempArr = _.where(itemsArr, {color: "red"}).concat(_.where(itemsArr, {color: "blue"}));
这是解决方案
var itemsArr = [{name:"foo", color:"red"}, {name:"bar", color:"blue"}, {name:"bob", color:"red"}];
var filter = ["red","blue"];
var data = {};
_.each(filter, function (item) {
data[item] = true;
});
var returnData = _.filter(itemsArr, function (item) {
return data[item.color];
});
console.log(returnData)
使用filter函数:
_.filter(itemsArr,function(itm){return itm.color=='red' || itm.color=='blue'})
_.filter(itemsArr, function(item, index) {
return _.contains(["red", "blue"], item.color);
})