What I am trying to accomplish, being able to use the filter function to filter out the people that have the age: 21 and the color: blue.
Any suggestions on how to make it work? Thanks in advance.
Here is my code:
var arr = [ { name: 'Steve', age: 18, color: 'red' }, { name: 'Louis', age: 21, color: 'blue' }, { name: 'Mike', age: 20, color: 'green' }, { name: 'Greg', age: 21, color: 'blue' }, { name: 'Josh', age: 18, color: 'red' } ];
var filter = function(arr, criteria){
var filtered = []
for(var i=0; i<arr.length; i++){
if (arr[i] === criteria) {
filtered.push(arr[i])
}
return filtered;
}
}
console.log(filter(arr, { age: 21, color: 'blue' }));
===compares object identity, instead of telling you wether the lhs has all the properties that the rhs has.