Javascript 使用下划线.js筛选数组
为了更好地理解JS,我正在尝试过滤一些对象,我正在使用下划线.JS 我来自C#背景,习惯LINQ,但下划线不太一样 你能帮我根据定义的测试筛选出这个数组吗?我遇到的问题是数组上的数组属性。Javascript 使用下划线.js筛选数组,javascript,underscore.js,Javascript,Underscore.js,为了更好地理解JS,我正在尝试过滤一些对象,我正在使用下划线.JS 我来自C#背景,习惯LINQ,但下划线不太一样 你能帮我根据定义的测试筛选出这个数组吗?我遇到的问题是数组上的数组属性。Where运算符与我通常用于筛选项目的C不同 products = [ { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false }, { n
Where
运算符与我通常用于筛选项目的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);
});
您只需删除
从拒绝
回调中选择code>,使其如下所示:
var noMushrooms = _(noNuts).reject(function(x){
return _(x.ingredients).any(function(y){return y === "mushrooms";});
});
否则,您将拒绝不包含蘑菇的,而不是包含蘑菇的。我设法将我的解决方案全部打包到一个筛选器调用中,因此我想发布它:
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);
});
实现这一点的更简洁的方法是使用下划线的chain()函数:
var noMushrooms = _(products).chain()
.filter(function (x) {
return !x.containsNuts;})
.reject(function(x){
return _(x.ingredients).any(function(y){
return y === "mushrooms";
});
})
.value();
这将得到期望的结果
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()
与filter()
相反,而any()
相当于某种数组方法,当数组中的任何元素通过回调返回true时,该方法返回true。DOH!谢谢你,我做到了。如果可能的话,你有什么想法来连锁吗?我已经试过了,但是幸运的是,你不需要先做\uu.chain(list)
,然后你就可以整天把你的函数链起来了,就像这样\u.chain(myArray).filter(function(){}).reject(function(){})
看起来像是。any()是去润滑的。下划线现在有一个u.some()方法,根据是否有“某些”匹配项返回true或false。@skrile它不是不推荐的,any
是的别名。谢谢@johnyhk-我今天学到了一些新东西:)您不需要链接,但通过这种链接方式可以非常清楚地知道您对列表执行的操作。