[英]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如您所见,它属于 arr 的第二个元素,并且必须以某种方式将其推入 el 属性,从而导致
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所以我知道我可以先过滤 arr 以获得我想要的部分,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最后我必须以某种方式在全局 arr 中替换它
Can someone lend a hand with this?有人可以帮忙吗?
You can use findIndex
to find the index of category_set_id: 44,
from arr
.您可以使用
findIndex
从arr
中查找category_set_id: 44,
的索引。 If arr
contains such object then it will return the index which will be greater than -1
.如果
arr
包含这样的 object 那么它将返回大于-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或者,您也可以使用
find
如果category_set_id
匹配,它将返回 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); 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所以首先我们要找到正确的 object 来插入我们的新元素,然后用正确的字段改变内部数组
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.您可以使用 find 来获得对搜索到的 object 的引用,然后您可以将元素插入(推入)到找到的 object 的 el 数组中。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.