Implement a Matching Object Filter - Implement a Matching Object Filter

Tell us what’s happening:

iam stuck here, and i feel confuse about this matching object filter,
i donno what do to,

any hint here

thank you all

Your code so far

function whatIsInAName(arrOfObjs, srcObj) {
     let srcKey = Object.keys(srcObj).join('')
     let srcValue = Object.values(srcObj).join('')
     let result = []
  //  arrOfObjs.forEach((obj, index) => {
  //   let objkeys = Object.values(obj)
  //   // console.log(objkeys)
  //   if(objkeys.indexOf(srcKeys) != -1) {
  //     result.push( arrOfObjs[objkeys.indexOf(srcKeys) + 1])
  //   }
    // console.log(objkeys.indexOf(srcKeys))

      // console.log(Object.values(arrOfObjs).includes(Object.keys(srcObj).join('')))
      for(const obj of arrOfObjs) {
        if(obj[srcKey] === srcValue){
          result.push({srcKey:obj[srcKey] })

        }
      console.log(obj[srcKey] == srcValue)
      }
      // Object.values(arrOfObjs).map(e => console.log(e))
    return result
}

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

Your browser information:

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

Challenge Information:

Implement a Matching Object Filter - Implement a Matching Object Filter
https://www.freecodecamp.org/learn/full-stack-developer/lab-matching-object-filter/implement-a-matching-object-filter

1 Like

Hi @SamehCode01

Start troubleshooting by console logging each of the variable in the for loop.

Happy coding

Hello, watching your code I realized that the logic your implement goes the right way. But, I think you shouldn’t use .join(‘‘) method, instead, do you need to extract in srcKey and srcValues the keys and values respectively. After that,

  1. For each obj of arrOfObjs check if it has all the keys,

1.1) If true: check if all the key-value pairs match.

1.2) If false: continue for the next obj.

  1. If all the key-value pairs match in the step 1.1, then push that obj to the result array.

That’s the logic that I implemented, and it worked.

1 Like

special thanks to you for your time and your efforts,
i will try to execute it as you advised me,
thanks again :slight_smile:

1 Like

there is a point here,
1.1 - how can i check for key value pairs ? should i use forEach(value, key)
for it
um…

Hi, you have to iterate over all “srcObj” keys, and then for every key you check if exist in “obj” of interest. You can implement nested for, the top for iterate over the objects of “arrOfObj”, and the inside for iterate over the keys of “srcObj”. I hope you get it.

i can’t get it still,

the srcObj keys is the indexes of the array like [0,1,2]

no, that’s not correct, if you are seeing that you are not accessing correctly

iam totally confused here

if i try to get keys of srcObj i get array containing ['first', 'last']

and you need to use those to check the objects in the array

1 Like

Please post your updated code.

there is no updated code, i deleted it because i was only able to match capulet test.

and i used join method and someone advised me not to use it , so i donno how to do it

Hi @SamehCode01

Start from the code you originally posted.

Try and work out what is happening. Spending time debugging code is a skill you’ll need to develop.

Happy coding

1 Like

yes, i will but first i think i need to re-read object lessons again,
maybe i find something i need to review or something like that
thanks for your advice .

1 Like

Sometimes it helps to write out the steps you think you need to take to solve the problem before writing any code.

i will try, thanks for enocurage me

1 Like

this is my week point i will try to work on it, thanks for pushing me up.

i think i started to feel despair

i always return to square zero,
i only can manipulate and match the first test case

and here are my updated code

function whatIsInAName(arrOfObjs, srcObj) {
     let srcKey = Object.keys(srcObj)
     let srcValue = Object.values(srcObj)
     let result = []

  //  arrOfObjs.forEach((obj, index) => {
    // let objkeys = Object.values(obj)
  //   // console.log(objkeys)
  //   if(objkeys.indexOf(srcKeys) != -1) {
  //     result.push( arrOfObjs[objkeys.indexOf(srcKeys) + 1])
  //   }
    // console.log(objkeys.indexOf(srcKeys))

        arrOfObjs.forEach(obj => {
          if(Object.values(obj).includes(srcValue.join())) {
            result.push(obj)
          }

        })
   
      for(const obj in arrOfObjs) {
        let objKeys = Object.keys(arrOfObjs[obj])
        let objVals = Object.values(arrOfObjs[obj])
          // for(let item in objKeys) {
          //   // console.log(objKeys[item])
          //   console.log(Object.values(arrOfObjs)[0].first)
          //   // console.log(arrOfObjs)
          // }
     
        }

      // console.log(obj[srcKey] == srcValue)
     return result
}
      // Object.values(arrOfObjs).map(e => console.log(e))


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

I think you shouldn’t use .join(‘‘) method

i really donno how to compare if i didn’t use join() method ?!!