1

我有以下问题。我想根据 fruits 数组过滤这个 fruitsCollection。我想得到的结果是,例如:

     filteredFruits1 [ all fruits with the 
                       exception of those which are in  
                       fruitsToCut array
                     ]

例子:

var fruitsToCut = [ 'egzotic', 'other'],
    fruitsCollection = [ {name: papaya, type: 'egzotic'}, 
                         {name: orange, type: 'citrus'}, 
                         {name: lemon, type: 'citrus'}
                       ]

也许一些下划线功能?

4

1 回答 1

3

在现代浏览器上,您可以使用 native filter

fruitsCollection.filter(function(fruit) {
  return fruitsToCut.indexOf(fruit.type) === -1;
} );

否则,您可以以几乎相同的方式使用下划线过滤器:

_.filter( fruitsCollection, function(fruit) {
  return !_.contains(fruitsToCut, fruit.type);
} );

此外,需要引用您的水果名称:

fruitsCollection = [ {name: 'papaya', type: 'egzotic'}, 
                         {name: 'orange', type: 'citrus'}, 
                         {name: 'lemon', type: 'citrus'}
                       ];
于 2013-10-02T06:39:26.860 回答