Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
How to push an object into an array nested in an array of objects?

So I have this arr

let arr = [{
 category_set_id: 25,
 name: "25name",
 el: [
   {name: "25name1", value: "25value1"}
 ]
},
{
 category_set_id: 44,
 name: "44name",
 el: [
   {name: "44name1", value: "44value1"}
 ]
},
]

And this is the element i need to push into this arr of objects

let elToPush = {category_set_id: 44, name: "44name2", value: "44value2"}

As you can see, it belongs in the second eleement of the arr, and it must somehow be pushed into the el property, resulting in

let arr = [{
 category_set_id: 25,
 name: "25name",
 el: [
   {name: "25name1", value: "25value1"}
 ]
},
{
 category_set_id: 44,
 name: "44name",
 el: [
   {name: "44name1", value: "44value1"},
   {name: "44name2", value: "44value2"}
 ]
},
]

So I know I can maybe first filter the arr to get my desired section, fe

let toModify = arr.filter(i => i.category_set_id === elToPush.id)

Then having that I could

let newarr = [toModify.el, ...elToPush]
toModify.el = newarr

And finally I must somehow replace it in the global arr

Can someone lend a hand with this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use findIndex to find the index of category_set_id: 44, from arr. If arr contains such object then it will return the index which will be greater than -1. Then use this index to push the required value

let arr = [{
    category_set_id: 25,
    name: "25name",
    el: [{
      name: "25name1",
      value: "25value1"
    }]
  },
  {
    category_set_id: 44,
    name: "44name",
    el: [{
      name: "44name1",
      value: "44value1"
    }]
  }
];

let elToPush = {
  category_set_id: 44,
  name: "44name2",
  value: "44value2"
};

const getIndex = arr.findIndex(item => item.category_set_id === elToPush.category_set_id);
if (getIndex > -1) {

  arr[getIndex].el.push({
    name: "44name2",
    value: "44value2"
  })

};

console.log(arr)

Alternatively you can also use find which will return the object if category_set_id matches

let arr = [{
    category_set_id: 25,
    name: "25name",
    el: [{
      name: "25name1",
      value: "25value1"
    }]
  },
  {
    category_set_id: 44,
    name: "44name",
    el: [{
      name: "44name1",
      value: "44value1"
    }]
  }
];

let elToPush = {
  category_set_id: 44,
  name: "44name2",
  value: "44value2"
};

const obj = arr.find(item => item.category_set_id === elToPush.category_set_id);
obj && obj.el.push({
  name: elToPush.name,
  value: elToPush.value
})
console.log(arr)

about 4 years ago · Juan Pablo Isaza Report

0

so first we want to find the right object to insert our new element in, and then mutate the internal array with the right fields

updateArray(newObject) {
  const elemToChange = arr.find(object => object.category_set_id == newObject.category_set_id)
  const objectToPush = { name: newObject.name, value: newObject.value }
  elemToChange.el.push(objectToPush)
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use find for having a reference to the searched object, and then you can insert (push to) the element in the el array of the found object.

let arr = [{
    category_set_id: 25,
    name: "25name",
    el: [{
      name: "25name1",
      value: "25value1"
    }]
  },
  {
    category_set_id: 44,
    name: "44name",
    el: [{
      name: "44name1",
      value: "44value1"
    }]
  }
];

let elToPush = {
  category_set_id: 44,
  name: "44name2",
  value: "44value2"
};

const obj = arr.find(item => item.category_set_id === elToPush.category_set_id);
if (obj) {

  obj.el.push({
    name: "44name2",
    value: "44value2"
  })

};

console.log(arr)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!