Tell us what’s happening:
I looked at the advanced code solution and I just don’t understand why reduce() is necessary for the filter() method to work. When I get rid of the reduce() part of the code, all that is returned is a copy of the array that filter was called on. It seems that map() in this case returns an array of boolean values so why doesn’t filter just get rid of all the instances that return false? Why does it need reduce() to work? Could someone explain this please?
Your code so far
return collection.filter(function (obj) {
return srcKeys
.map(function(key) {
return obj.hasOwnProperty(key) && obj[key] === source[key];
})
.reduce(function(a, b) {
return a && b;
});
});
function whatIsInAName(collection, source) {
// What’s in a name?
var arr = ;
// 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” });
**Your browser information:**
User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36</code>.
**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/