Implement a Matching Object Filter - Implement a Matching Object Filter

Tell us what’s happening:

I need some hints, I’m not getting it and not sure where to go or what to change. Thanks in advance!

Your code so far



const whatIsInAName = (arr, obj) => {
  let objKeys = Object.keys(obj) // extract "keys" from obj

  objKeys.forEach(i => { // iterate over obj
     arr.filter(item => {
        const keys = Object.keys(item) // extract keys from arr
   
      console.log(keys, i)
      if(keys.includes(i)){ // compare keys from arr and obj
        return item // return the object in the array
    }
  })
  
})
}

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

// console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }))

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




 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0

Challenge Information:

Implement a Matching Object Filter - Implement a Matching Object Filter

Can you talk about what it is you need to do in this challenge? Don’t try to code it yet. Just say what steps you think you need to take to solve this challenge.

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

{ “apple”: 1, “cookie”: 2 })) has to return

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

What I understand is that I have to take the object that is passed through and match the key:value pair/pairs from that object to an object in the array.

What I have been trying to do but unsuccessfully is seperate the keys from the arr and the keys from the obj and compare them.

did you try to research methods to check object keys? that could be a good starting point for you

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