Hey. I’m doing Wherefore art thou challenge and don’t quite understand one thing. I’m getting value from source object (“last”) and then I compare it with “collection” value but somehow it returns all objects. Why? Meanwhile when I just put “last” in "if (elem.keysSource == source.keysSource) instead of “keysSource” variable it works like a charm. Could you give me a hint on why it’s happening?
function whatIsInAName(collection, source) {
var arr = [];
var keysSource = Object.keys(source).join('');
collection.filter(function(elem){
if (elem.keysSource == source.keysSource) { // if (elem.last == source.last) works like it should
arr.push(elem);
}
});
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });