简体   繁体   中英

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?

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)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM