Wherefore art thou?

Wherefore art thou?
0

#1

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" });

#2

Neither elem nor source has the keysSource property, meaning both are undefined and equal. Always equal, so no filtering happens.


#3

But why it’s undefined? When I do “return keysSource” it gives me “last” as intended…


#4

no “keySource” in any of them, only “first” and “last”:
{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” }

What you’re printing is the variable you created.


#5

vs
elem[keysSource]

Since you want it evaluated.


#6

Yes, that’s what I’ve been looking for! Thanks!