Object.prototype.hasOwnProperty method not working on this

Tell us what’s happening:
Describe your issue in detail here.
I tried using the object.prototype.hasOwnProperty filter method. But it’s not working. Can you help me in completing this test.
Your code so far


function whatIsInAName(collection, source) {
let arr = [];
// Only change code below this line
let filterArr = collection.filter(value => {
  return value.hasOwnProperty(source);
})
arr.concat(filterArr);
// 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/90.0.4430.212 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

note that source is something like this, so it can’t be a property of one of the elements in collection

1 Like

To solve your problem, try to answer these question:

  • What the type of source?
  • What’s the syntax for filter (including what does the method return)?
  • What is the challenge asking you to compare?
  • What is the challenge asking you to return?
1 Like

I got stuck. Got only 2 tests correct at a time.
If use

arr = collection.filter(value => {
for(let i = 0; i < Object.keys(source).length; i++) {
return value.hasOwnProperty(Object.keys(source[i]));
}
}
return arr;

I got the 2nd and 3rd test correct.
And if I do,

arr = collection.filter(value => {
return Object.values(value).includes(Object.values(source).join(""));
})

I got only the 1st and last test correct.
Can you help me. Where am I doing wrong?

I’ll give another hint: use for...in

1 Like

Thanks for suggesting it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.