This is the solution. But according to this solution, in the first if statement, the second or condition translates to obj[first] in the first example which translate to the value of the first property. But when I try let person = { first: "Romeo", last: "Montague" }; console.log(person[first]); in a code editor I get an error.
function whatIsInAName(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.β
// -- by William Shakespeare, Romeo and Juliet
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function(obj) {
for (var i = 0; i < srcKeys.length; i++) {
if (
!obj.hasOwnProperty(srcKeys[i]) ||
obj[srcKeys[i]] !== source[srcKeys[i]]
) {
return false;
}
}
return true;
});
}
Ah so the reason this works in the solution is because the .keys() method is storing an array of strings. So obj[srcKeys[i]] equates to obj["first"] in the first example right?
Exactly right. srcKeys is an array of string values, each string being one of three property names on the src object. srcKeys[0] is one of those property names, might be "first" or might not. There is no hard rule about how property names get ordered when pulled by Object.keys()