Filtering array with underscore.js

后端 未结 4 1931
囚心锁ツ
囚心锁ツ 2021-01-14 01:44

I am trying to filter some objects in my attempt to understand JS better and I\'m using underscore.js

I come from a C# background and am used to LINQ however undersc

4条回答
  •  轮回少年
    2021-01-14 01:55

    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.

提交回复
热议问题