Using an object variable to look for an matching object inside an array

hey campers! getting confused again trying stuff out in a pad cause sometimes its nice to go away from the questions and just play with stuff to understand how its working below is my code and basicly im wondering if what im trying to figure out is possible or if im misunderstanding something else

let obj = [{name:"name",
           age:20,},
           {name:"bill",
           age:16,},
           {age:20}
           ]  

let com = {name:"bill"}

let filterArr = obj.filter( (x) => x.hasOwnProperty(["name"])  )           //this works and only shows the entires with a name feild, but i want to replace ["name"] with the com variable many thanks
//this works and only shows the entires with a name feild, but i want to replace ["name"] with the com variable many thanks

ah sorry if i badly explained what i want to do is be able to use a placeholder in the working code above instead of a known feild for instance the code above only prints into console the objects with the name feild and not the the which is only age. i want to replace the ([“name”]) to ([com]) so it will filter based on a variable or arugment/parameter.

to be honest i have diffeculty going tho a thinking process to access objects in arrays and when its doing something with an object to an object in an array i just fall flat

yes sure

let filterArr = obj.filter( (x) => x.hasOwnProperty(["name"])  )

the line above filters out the object which does not have a name property, i want to change it to something which uses the variable

let filterArr = obj.filter( (x) => x.hasOwnProperty([com])  )

https://playcode.io/363289?tabs=script.js,preview,console theres a link the first .filter() works as intended the second always just places an empty array instead of doing the same or similar thing

yes all i was asking as for a way to not have to specify the key of the object so it can be reusable in other in searches

ok i figured it out, there is an Object.keys method which you can call on objects to put each key into an array only took 2days of confusion online wheyy i suck lol

1 Like

Why do you say so? You were able to find the answer! You are awesome! Aren’t you awesome? Yes you are! You managed to solve a great challenge!

1 Like