Wherefore Art Though

Tell us what’s happening:

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }] , and the second argument is { last: "Capulet" } , then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.

Your code so far

function whatIsInAName(collection, source) {
  var arr = []; 
  let secondObj = Object.entries(source);
    for (let elem of collection){
      let singleObj = (Object.entries(elem)); 
      for (let testElement of secondObj){
        for (let elem2 of singleObj){
          console.log(elem2);
          let string = testElement.toString();
          let string2 = elem2.toString();
        console.log(elem2, testElement, string2 === string);
        if (string2 === string){
          arr.push(Object.fromEntries(new Map(testElement)));
        }
      }
    }
  }    return arr; 
}

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

Why am I getting type error when I have done a fair amount of research on object comparison and I have applied to the above code? What is different above?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15.

Challenge: Wherefore art thou

Link to the challenge:

what’s the whole error you are getting?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That’s strange for me it appears as code block would appear in editors.

TypeError: Type error

Thats the entire error message - the code does work as I ran console.log on the IF statement but not sure why its not pushing in empty array

Also tried JSON.stringify(arr1) === JSON.stringify(arr2) without any luck

this line gives the error

also, I see more of the error: TypeError: Iterator value last is not an entry object

specifically the error is from new Map(testElement)

you may want to read about Map

It was the incorrect input in the Object.fromEntries() method - it needs to be a 2D array. This has been rectified. Only thing I need to correct now is if the source object has more than one KV pair? will this work with my for...of approach?
I am still testing my code to account for this.

provide your updated code please

Please see below:

function whatIsInAName(collection, source) {
  var arr = []; 
  let secondObj = Object.entries(source);
    for (let elem of collection){
      let singleObj = (Object.entries(elem)); 
      for (let testElement of secondObj){
        for (let elem2 of singleObj){
          let string = testElement.toString();
          let string2 = elem2.toString();
        //console.log(elem2, testElement, string2 === string);
        if (string2 === string){
          arr.push(Object.fromEntries(new Map(singleObj)));
        }
      }
    }
  }    return arr; 
}

this is one of the tests that fail:

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

the output should be

[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]

instead your function returns

[ { apple: 1, bat: 2 },
  { apple: 1, bat: 2 },
  { bat: 2 },
  { apple: 1, bat: 2, cookie: 2 },
  { apple: 1, bat: 2, cookie: 2 } ]

those are the objects of the input, even duplicated - the ones duplicated are the ones to keep, but maybe you need to rebiew where you are putting them