Wherefore art thou iterating help

Wherefore art thou iterating help
0

#1

I seem to be getting a comparison of the key names only. I asked for help on the chat and I was told that I need to iterate through the keys as well. I don’t understand how I’m supposed to do that. The only way I can figure out how to get to the values is by hardcoding the key name, something like source.last. I can’t use a variable in bracket notation. I was thinking of using filter, but I don’t understand how to use filter for something like this. Please help, while my MacBook Pro is still in one piece! :frowning:

My code:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [],
      sourceKeys = Object.keys(source);
  // Only change code below this line
  
  for ( var i = 0; i < collection.length; i++ ) {
    if ( collection[i].hasOwnProperty(sourceKeys) ) { 
      arr.push(collection[i]);  
    }
  }
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });


#2

Hi! Why don’t you try with the fliter( ) and every( ) methods ? You can store the object keys in a variable and use it later


#3

Try to make a 2nd loop for sourceKeys, inside the 1st loop(collection)