I'm trying to understand the logic behind this FCC activity. I'm puzzled as to why the code is using keys.length instead of collection.length to iterate over the Array of objects. In this example, keys.length is only 1 while the collection array has 3 objects. How do we go through the entire array in this scenario? Could it be that the if statement inside the loop is causing the i value not to increment until the condition becomes true? I appreciate any help in clarifying this for me as a beginner! :)
function whatIsInAName(collection, source) {
let keys = Object.keys(source)
let arr = collection.filter(function(obj) {
for (let i = 0; i < keys.length; i++) {
if (!obj.hasOwnProperty(keys[i]) || obj[keys[i]] !== source[keys[i]]) {
return false
}
}
return true
})
console.log(keys.length)
// Only change code below this line
// Only change code above this line
return arr;
}
whatIsInAName([{
first: "Romeo",
last: "Montague"
}, {
first: "Mercutio",
last: null
}, {
first: "Tybalt",
last: "Capulet"
}], {
last: "Capulet"
});