Wherefore art thou - Need Help

Wherefore art thou - Need Help
0

#1

Tell us what’s happening:

I can’t get it done. Don’t know how to compare the property name and value both.

Your code so far


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

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

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.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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


#2

Hi :slight_smile:

var srcKeys = Object.keys(source);

That is a good first step. Now we have an array with all the properties that have to be inside the test case object.

Inside your filter callback function you have to test if the obj object has each of srcKeys properties and the same value as the source object. Therefore you could iterate over the srcKeys array.

for (let i = 0; i < srcKeys.length; i++) {}

In order to compare the object values you have to use the Bracket notation.

obj[srcKeys[i]] !== source[srcKeys[i]]

I hope I could help you with it. Good luck.


#3

Thanks. I solved the problem with filter and every functions.