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

So you want to replace all name properties’s values in the objects of the array (obj - consider renaming this to arr), with “bill” if the name property value is “name”? You are currently using filter which does not change the objects inside the array but just keeps the objects when the callback function returns the value true. You could use reduce or map to accomplish what you want. I am reformatted your array (and renamed it) below to make everything more readable.

const arrayOfObjects= [
  {
    name:"name",
    age:20,
  },
  {
    name:"bill",
    age:16,
  },
  {
    age:20
  }
];

const modifiedArray = arrayOfObjects.map(obj => {
  if (obj.name === "name") {
    obj.name = "bill";
  } 
  return obj;
}); // [ { name: 'bill', age: 20 },  { name: 'bill', age: 16 },  { age: 20 } ]

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

Can you show what you are expecting the final array to look like after it is filtered? I am not sure what you are trying to do here. Maybe provide a couple of before and after examples.

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])  )

You misunderstood what I asked. Show me examples of what various arrays would look like before and after you have applied your filter (or map or whatever we decide is the best course of action).

FYI - hasOwnProperty accepts one argument (should be a string). In the two examples you posted above, you are trying to pass an array ([“name”] is an array and [com] is an array with one element which has a value of whatever the com variable stores).

Does the following do what you want?

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

If you want to filter objects which have a name property with a specific name like “bill” (represented by a variable com), you would write it as:

const arrayOfObjects= [
  {
    name:"name",
    age:20,
  },
  {
    name:"bill",
    age:16,
  },
  {
    age:20
  }
];

const com = "bill";
const modifiedArray = arrayOfObjects.filter(obj => obj.name === com);

console.log(modifiedArray); // [ { name: 'bill', age: 16 } ]

The code you posted in the playcode.io link filters all objects in the array which have a “name” property. which is why the 3rd element of the original array was not included. The same would have been accomplished without the [ ] surrounding the “name”.

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

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