Wherefore art thou -- returning wrong results

My code below returns array contain of every object that has a matching name instead of those with both matching name and value.
Please tell me what’s wrong with it.

Your code so far


function whatIsInAName(collection, source) {
 var arr = [];
 // Only change code below this line
 var arg = Object.keys(source);

for (var i = 0; i < collection.length; i++){
arr = collection.filter(value => value.hasOwnProperty(arg)); 
}

 // 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: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

that’s what’s your function does, keeps the objects that has property arg (remember that arg is an array so it is working right now only because arg has one single element inside. If it has more like in the other tests it doesn’t work.

also, note this is in a loop but you never use i so you are just doing multiple times the same operation

this is an algorithm with a bit of complexity, do it one step at a time. it will take time. it’s normal.

So the for loop is unnecessary?
I think I’m more confused now.

Finally figured it out. Thanks


function whatIsInAName(collection, source) {

  var arr = [];

  // Only change code below this line

  var arg = Object.keys(source);

arr = collection.filter(function(value) {

    for (var i = 0; i < arg.length; i++){

     if ( !value.hasOwnProperty(arg[i]) || value[arg[i]] !== source[arg[i]]){

       return false;

     }

    }

    return true;

});

   // Only change code above this line

  return arr;

}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });