This somehow works for the first two cases, but it’s not working for the last 2 cases.
function arrContainsObj(arr, obj){
for (var i=0; i < arr.length; i++){
if(arr[i] == obj){
return true;
}
}
return false;
}
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
collection.forEach(function(val){
var keys = Object.keys(source);
for (var i=0; i < keys.length; i++){
if (val.hasOwnProperty(keys[i]) && val[keys[i]] == source[keys[i]]){
if (!arrContainsObj(arr, val)){
arr.push(val);
}
}
}
});
// Only change code above this line
return arr;
}
whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 , "c" : 2});