Iterating through properties of objects that are in an array

Iterating through properties of objects that are in an array
0

#1

Hello, I am having a hard time solving Wherefore art thou challenge, here’s my code so far :

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  console.log(JSON.stringify(
  collection.filter((obj) =>{ return obj.filter
  (function(property) {if(obj[property] === source[property]){
      return obj}}) } )))

  // Only change code above this line
  return arr;
}

console.log(JSON.stringify(whatIsInAName([{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” })));;`

So the idea so far is that I should use array.prototype.filter method for this challenge and the problem I had with it is that I couldn’t figure out how to pass property of an object as a variable without hard-coding it. So I thought maybe I could go a layer deeper and iterate through the properties instead of just objects, so that’s what the second .filter function is for. However I get an error obj.filter is not a function. How can I avoid that and should I be using filter here at all?
Link to the challenge : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/

Edit : Found out that using [obj].filter solves the error but I still can’t solve the challenge


#2

You are on the right track with filter, but remember it only works when you provide a condition that returns a Boolean (true or false) and will append all true instances to your new list.

So your condition needs to be clearer and the syntax needs to be tidied up a bit - you don’t need the second filter in there (it’s not working because filter is for lists and you are applying it to an object)