Implement a Matching Object Filter

Is .includes() the right comparison method here? Why don’t apple, bat, cookie get pushed in?

function whatIsInAName(arrObj, sourceObj){
  let newArrPush = []; 
  for (let obj1 of arrObj) {
    //console.log(obj1);
    //console.log(sourceObj);
    let obj1String = JSON.stringify(obj1);
    let sourceObjString = JSON.stringify(sourceObj);

    //let obj1String = Object.entries(obj1);
    //let sourceObjString = Object.entries(sourceObj);

    if (obj1String.includes(sourceObjString)) { 
        newArrPush.push(obj1);
    }    
  }
  return newArrPush;
}

console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));

Have you looked at the stringified values?

yes, that’s why i’m puzzled why it doesn’t work

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

2 Likes

Here’s example with different keys and values, aligning strings to be one under the another might help:

JSON.stringify({one: 1, two: 2})            // '{"one":1,"two":2}'
JSON.stringify({one: 1, two: 2, three: 3})  // '{"one":1,"two":2,"three":3}'

What will be the result of '{"one":1,"two":2,"three":3}'.includes('{"one":1,"two":2}') ?

Even if you remove the curly braces from both strings, this approach will never work to pass this test:

whatIsInAName([{ “apple”: 1, “bat”: 2 }, { “apple”: 1 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }], { “apple”: 1, “cookie”: 2 })should return[{“apple”: 1, “bat”: 2, “cookie”: 2}]

Do you see why?

I ran it so I know it’s false but don’t know why.

I can see that the strings still won’t match because the obj key pairs are not in the same order as the array of objects. I’m still stuck though. Have tried Object.entries, keys && values.

1 Like

Could you share/update the code with your different approach?

Consider this as a second loop option:

for…in - JavaScript | MDN

2 Likes

and here is a hint :wink:

limme ask you if it’s for...in or for...of

Yes. Look at the for...in loop. See the link referenced above.

2 Likes

i used for in and now i can see each object values but i donno how logically i can compare it

Hey @SamehCode01,

if you have troubles with this challenge, please start new thread. Preferably by using the Get HelpAsk for Help buttons on the challenge page.

1 Like

Ok I now understand that they also don’t match because of (or lack of) the curly braces. Took only like 11 days to figure that out lol

And I finally solved it. I only took 11 days to decide I should probably revisit the fundamentals based on the hints @dhess gave. Feel like a sense of achievement lol

you’re genius then, iam more than 22 days and don’t know what to do yet

Took me 3 - 4 hours of intense focus to get this one… It really, really helps if you read it out loud. And also walk away from the problem and come back.

Instruction 2 reads: *The whatIsInAName function should return a new array containing only the objects from the collection that have all the key–value pairs present in the source object.
*
This is telling us the collection (the first parameter containing all the objects in an array) needs to be filtered so that every element in the source object is contained in the collection.

It confused me especially, because I was so stuck on chaining and I really underestimated what these higher order functions could do. I ended with so much commented out code just in case I wanted to go back to it, but then it all came down to a one line answer. Nested higher order functions, destructuring, and Object methods all in one… felt like an early capstone.

This also just caught me off guard. There were some sorta tough ones before this, but none that took me this long to solve. Hopefully my mind is molded for future problems now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.