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.

1 Like

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 @fcc4b6d10c4-b540-4e2 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