The code just returns what is in the first argument that matches the second argument and it checks out. link just in case
My main focus is "source[srcKeys[i]]" in the if statement. "srcKeys" alone returns "['last']" but if you add 'source' in front of it such as "source[srcKeys]" you get "Capulet". My question is why its returning "Capulet" and not "['last']" since it targets the Object.keys and not the value?
function whatIsInAName(collection, source) {
var srcKeys = Object.keys(source);
return collection.filter((obj) => {
for (var i = 0; i < srcKeys.length; i++) {
if (obj[srcKeys[i]] !== source[srcKeys[i]]) {
return false
}
}
return true;
});
}
whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }
],
{ last: "Capulet" }
);