Wherefore art thou -

Here’s what I have so far:

function whatIsInAName(collection, source) {
  var filtered = collection.filter(value => value.last == Object.values(source));
  return filtered;

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

How can I make ‘value.last’ dynamic? The following loop gives me what I want, but I don’t know how to implement it into the code above.

  for (var i = 0; i < collection.length; i++) {
    var names = Object.keys(collection[i]);

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

@procyon82 I am not understanding what you mean by value.last here. The last object in the collection array? Remember that Object.values(source) is going to be an array. You can not compare arrays to other arrays or other things using the == operator. Can you explain how you would like to use the code in the for loop with respect to value.last? Then, maybe we can help guide you to a solution.

@RandellDawson Well if I do console.log(source.last) it gives me “Capulet” and changing it to Object.values(source) makes it dynamic.

var filtered = collection.filter(value => value.last == Object.values(source));

The code above works for the first test, but fails on the second one as the keys have different names:

whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 });

I could manually adjust it to value.apple, but obviously I don’t wanna do that. I want to have a placeholder in there that will dynamically get the right key name:

var filtered = collection.filter(value => value.apple == Object.values(source));

The only reason you passed the first test was pure coincidence.

When you made the comparison of value.last == Object.values(source), even though Object.values(source) was an array, it got coerced into a string, because value.last is was a string.

The filter method is a good start, but I suggest you try to break the problem down into smaller pieces. You know you will need to validate that each object in the collection array meets both of the following conditions:

  • the same property name exists
  • the property’s values match

The property names are the keys in each collection object and the keys of the source object. I would think about how you would use the Object.keys method to capture both of these. Then, within the filter method you would want to iterate through the source keys and check that every source key has a corresponding property of the same key and the value of each source key matches the value of each collection object key. There are a couple ways of doing this, but basically the following is one way (in pseudo-code to do it):

function whatIsInAName(collection, source) {
  var filtered = collection.filter(object => {
    var sourceKeysArr = // capture source keys
    var objectHasAllSourceKeys = true; // assume all will be found
    // iterate through sourceKeys

     if current sourceKey is not a property of object OR the value of the collection object's key is not equal to the value of the source object's key {
       objectHasAllKeys = false;
       stop iteration
     else {
      go to next object in collection or stop iterating if at end of collection array
   // end of iteration
   if objectHasAllSourceKeys is true then return true so the current object will be captured in the final filtered array.
  return filtered;

Keep in mind the above is just written logic and not the actual code you would write to solve the task. Some of it may be directly used. I wanted to give you an algorithm without giving you the actual code for the logic.

Appreciate the thorough response! Gonna give it another shot later today.