Can't understand how hasOwnProperty works and why my code isn't working

Tell us what’s happening:

I passed the test with the current (collection,source) but the code didn’t work out with " whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }] "

I did a console.log for the keys variable and it gave me [‘apple’,‘bat’] but when I use collection[i].hasOwnProperty(keys) it gives me false, what I don’t understand is why it gives me true when I write collection[i].hasOwnProperty(‘apple’,‘bat’), is it because the “keys” variable is an array? If that’s the case what should I do to convert it so array.hasOwnProperty() considers it true?

Thank you.

Your code so far


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

console.log(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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

keys is an array. hasOwnProperty() takes a string as a parameter.

Thank you, then that’s the issue as I thought, I tried using .toString on the keys variable and it changed to apple,bat but the ‘collection’ array still doesn’t take it as its own property. What’s the issue?

keys is an array of strings. You want to treat it like an array, not cast the array to a string.