使用underscore.js过滤数组

时间:2013-01-15 14:09:39

标签: javascript underscore.js

我试图过滤一些对象,试图更好地理解JS,我正在使用underscore.js

我来自C#背景并习惯LINQ,但下划线并不完全相同。

你能帮我根据定义的测试筛选出这个数组,我遇到的问题是数组上的数组属性。 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);
  });

4 个答案:

答案 0 :(得分:10)

您只需要从!回调中移除reject,使其如下所示:

var noMushrooms = _(noNuts).reject(function(x){ 
    return _(x.ingredients).any(function(y){return y === "mushrooms";});
});

否则你拒绝包含蘑菇而不是那些蘑菇的那些。

答案 1 :(得分:6)

更简洁的方法是使用下划线的chain()函数:

var noMushrooms = _(products).chain()
    .filter(function (x) { 
        return !x.containsNuts;})
    .reject(function(x){ 
        return _(x.ingredients).any(function(y){
            return y === "mushrooms";
        });
    })
    .value();

答案 2 :(得分:4)

我设法让我的解决方案全部包含在一个过滤器调用中,所以我想发布它:

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);
  });

答案 3 :(得分:4)

这将给出所需的结果

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。