Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to filter list of objects by an array in javascript

var array = ['cat','dog','fish','goat']  
var objects = [{pet:'cat', color:'brown'},{pet:'dog', color:'black'},{pet:'gecko', color:'green'}]

//finds the objects that match something in the list with the key pet.
var filteredObjects = objects.filter(function(obj){
    return array.indexOf((obj.pet).toString()) > -1;
  });
console.log(filteredObjects)
Source by sebhastian.com #
 
PREVIOUS NEXT
Tagged: #filter #list #objects #array #javascript
ADD COMMENT
Topic
Name
7+9 =