Javascript 使用 underscore.js 过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14339523/
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
Filtering array with underscore.js
提问by Jon
I am trying to filter some objects in my attempt to understand JS better and I'm using underscore.js
我试图过滤一些对象以更好地理解 JS,我正在使用 underscore.js
I come from a C# background and am used to LINQ however underscore is not quite the same.
我来自 C# 背景并且习惯于 LINQ 但是下划线并不完全相同。
Can you help me filter out this array based on the defined test, the issue I'm having is the array property on the array. The Whereoperator is diffeernt to C# which is what I'd normally use to filter items.
你能帮我根据定义的测试过滤掉这个数组吗,我遇到的问题是数组上的数组属性。该Where操作员diffeernt到C#这就是我通常用它来筛选项目。
products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {
var productsICanEat = [];
//This works but was hoping I could do the mushroom check as well in the same line
var noNuts = _(products).filter(function (x) { return !x.containsNuts;});
var noMushrooms = _(noNuts).reject(function(x){ return !_(x.ingredients).any(function(y){return y === "mushrooms";});});
console.log(noMushrooms);
var count = productsICanEat.length;
expect(productsICanEat.length).toBe(count);
});
回答by JohnnyHK
You just need to remove the !from the rejectcallback so that it look like this:
您只需!要从reject回调中删除,使其看起来像这样:
var noMushrooms = _(noNuts).reject(function(x){
return _(x.ingredients).any(function(y){return y === "mushrooms";});
});
Otherwise you're rejecting the ones that don'tcontain mushrooms instead of those that do.
否则,你拒绝了那些不含有蘑菇,而不是那些做。
回答by BishopZ
A more concise way to accomplish this would be with underscore's chain() function:
完成此操作的更简洁方法是使用下划线的 chain() 函数:
var noMushrooms = _(products).chain()
.filter(function (x) {
return !x.containsNuts;})
.reject(function(x){
return _(x.ingredients).any(function(y){
return y === "mushrooms";
});
})
.value();
回答by dinesh_malhotra
This will give the desired result
这将给出所需的结果
var no_nuts = _.filter(products,function(item) {
return !item.containsNuts;
});
var no_mushroom = _.reject(no_nuts,function(item) {
return _.any(item.ingredients,function(item1) {
return item1 === "mushrooms"
});
});
console.log(no_mushroom);
reject()does the opposite of filter(), and any()is equivalent to some method of arrays which returns true when any of the element in the array when passed through a callback returns true.
reject()与 , 做相反的事情filter(),any()相当于数组的一些方法,当数组中的任何元素通过回调返回真时,该方法返回真。
回答by Jon
I managed to get my solution all wrapped up into one filter call so thought I'd post it:
我设法将我的解决方案全部打包到一个过滤器调用中,所以我想我会发布它:
products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {
var productsICanEat = [];
productsICanEat = _(products).filter(function (x) { return !x.containsNuts && !_(x.ingredients).any(function(y){return y === "mushrooms";});});
expect(productsICanEat.length).toBe(1);
});

