Javascript 基于其他数组的筛选器集合(对象数组)
我有以下问题。我想根据水果数组过滤这个水果集合。 我希望结果是,例如:Javascript 基于其他数组的筛选器集合(对象数组),javascript,arrays,collections,underscore.js,Javascript,Arrays,Collections,Underscore.js,我有以下问题。我想根据水果数组过滤这个水果集合。 我希望结果是,例如: filteredFruits1 [ all fruits with the exception of those which are in fruitsToCut array ] 例如: var fruitsToCut = [ 'egzotic', 'other'],
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'}
]
也许是一些下划线函数 在现代浏览器上,您可以使用本机: 否则,您可以以几乎相同的方式使用:
_.filter( fruitsCollection, function(fruit) {
return !_.contains(fruitsToCut, fruit.type);
} );
此外,还需要引用您的水果名称:
fruitsCollection = [ {name: 'papaya', type: 'egzotic'},
{name: 'orange', type: 'citrus'},
{name: 'lemon', type: 'citrus'}
];
谢谢:)-它工作得很好,我必须再次阅读所有这些下划线函数。是的,我当然忘了水果名称的引号;)
fruitsCollection = [ {name: 'papaya', type: 'egzotic'},
{name: 'orange', type: 'citrus'},
{name: 'lemon', type: 'citrus'}
];