I'm struggling with this code and can't seem to figure out what's wrong. For some reason, the line "arrKeys = Object.keys(source);" is not returning the array as expected.
function findMatchingValues(collection, source) {
var arr = []; // should return an array of properties and values found in the object
var arrkeys = [];
arrKeys = Object.keys(source); // should return ["last"]
var test = false;
// Loop through the collection array object and search for objects that match the second argument passed to the method
for(var i = 0; i < collection.length; i++){
for(var j = 0; j < arrKeys.length; j++){
if(collection[i].hasOwnProperty(arrKeys[j])){
if(collection[i][arrKeys[j]] === source[arrKeys[j]]){
test = true;
}else{
break;
}
} else{
break;
}
if(j === (arrkeys.length - 1) && test === true){
arr.push(collection[i]);
}
}// end of inner for loop
}// end of outer for loop
return arr;
}
findMatchingValues([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });