callback that in the filter function executes for each object separately. So reduce will be invoked on array with only one element (and in this case reduce returns just that element)
you just can break the algorithm down in smaller pieces and print out the results to understand how it works
function whatIsInAName(collection, source) {
const souceKeys = Object.keys(source);
console.log("source keys:", souceKeys);
return collection.filter((obj, i) => {
console.log("current obj at index", i, obj);
let res = souceKeys.map(key => obj.hasOwnProperty(key) && obj[key] === source[key]);
console.log("map result:", res);
res = res.reduce((a, b) => a && b);
console.log("reduce result:", res);
console.log("----------------------");
return res;
});
}
whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }
],
{ last: "Capulet" }
);