admin管理员组文章数量:1026381
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'}
];
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'}
];
本文标签: javascriptFilter collection (array of objects) based on other arrayStack Overflow
版权声明:本文标题:javascript - Filter collection (array of objects) based on other array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745645041a2160975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论