@JohnL3 -Thanks, as you suggested i added another loop to loop through testkey and logically it must give right result , after checking for equality of both property and its value , still it returns something like this -
[{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 },{ "a": 1, "b": 2 },{ "a": 1, "b": 2, "c": 2 }]
I tried many combinations of loop but still no success …
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var testkey = Object.keys(source);
for(var j = 0; j < testkey.length; j++){
for(var i = 0; i < collection.length; i++){
if(collection[i].hasOwnProperty(testkey[j])){
if(collection[i][testkey[j]]=== source[testkey[j]]){
arr.push(collection[i]);
}
}
}
}
// 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 });