Wherefore art thou... help plz

Tell us what’s happening:

This is the start of the simplest approach I’m trying to do, but it’s returning nothing.
I’m hoping to loop through the objects in collection, and if an object has source (hasOwnProperty), I want to push the object to arr.

I realize that one issue is that source can have more than one key, value pair.

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
for (var i = 0; i < collection.length; i++){
if (collection[i].hasOwnProperty(source)){
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

remember that source is an other object, so you can’t just do this

1 Like

source is an object. Remember object properties are strings, so when you attempt to check hasOwnproperty(source), source gets coherced into a string that looks something like:

'[object Object]'

So you are checking if a property named [object Object] exists in the collection[i] object.

You are going to need to rethink your logic here.

1 Like