I have the following problem. I wolud like to filter this fruitsCollection based on fruits array. I would like to be the result was that, for example :
filteredFruits1 [ all fruits with the
exception of those which are in
fruitsToCut array
]
Example:
var fruitsToCut = [ 'egzotic', 'other'],
fruitsCollection = [ {name: papaya, type: 'egzotic'},
{name: orange, type: 'citrus'},
{name: lemon, type: 'citrus'}
]
Maybe some underscore function?
I have the following problem. I wolud like to filter this fruitsCollection based on fruits array. I would like to be the result was that, for example :
filteredFruits1 [ all fruits with the
exception of those which are in
fruitsToCut array
]
Example:
var fruitsToCut = [ 'egzotic', 'other'],
fruitsCollection = [ {name: papaya, type: 'egzotic'},
{name: orange, type: 'citrus'},
{name: lemon, type: 'citrus'}
]
Maybe some underscore function?
Share Improve this question asked Oct 2, 2013 at 6:34 AgataAgata 5541 gold badge9 silver badges18 bronze badges1 Answer
Reset to default 4On a modern browser, you can use the native filter
:
fruitsCollection.filter(function(fruit) {
return fruitsToCut.indexOf(fruit.type) === -1;
} );
Otherwise, you can use the underscore filter in pretty much the same way:
_.filter( fruitsCollection, function(fruit) {
return !_.contains(fruitsToCut, fruit.type);
} );
Also, your fruit names need to be quoted:
fruitsCollection = [ {name: 'papaya', type: 'egzotic'},
{name: 'orange', type: 'citrus'},
{name: 'lemon', type: 'citrus'}
];
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645042a4637920.html
评论列表(0条)