Wherefore art thou - Need Help

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

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.

1 Like

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

thought this one line solution was neat

function whatIsInAName(collection, source) {
  return collection.filter(obj => {return Object.keys(source).every(x => Object.keys(obj).includes(x)) && Object.values(source).every(x => Object.values(obj).includes(x))});
}

Please do not revive two year old topics just to post solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like