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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

On 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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

On 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