我有以下问题。我想根据 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'}
]
也许一些下划线功能?
最佳答案
在现代浏览器上,您可以使用 native filter
:
fruitsCollection.filter(function(fruit) {
return fruitsToCut.indexOf(fruit.type) === -1;
} );
否则,您可以使用 underscore filter以几乎相同的方式:
_.filter( fruitsCollection, function(fruit) {
return !_.contains(fruitsToCut, fruit.type);
} );
此外,您的水果名称需要引用:
fruitsCollection = [ {name: 'papaya', type: 'egzotic'},
{name: 'orange', type: 'citrus'},
{name: 'lemon', type: 'citrus'}
];
关于javascript - 基于其他数组过滤集合(对象数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19131187/