简体   繁体   English

如何在Javascript中更新对象数组的嵌套对象?

[英]How to update nested object of array of objects in Javascript?

const testArr = [
    { id: 'aaa', 
      children: [ 
           { id: 'aaa-1', 
             children: [
                { id: 'aaa-1-1' }   
             ] 
           }, 
           { id: 'aaa-2'}
       ] 
    },
    { id: 'bbb' },
]

I am having nested array of objects to managa my redux store. 我有嵌套的对象数组managa我的redux商店。 I want to add new property to object. 我想向对象添加新属性。 Let's add children to id: aaa-2 , the object will be below: 让我们将children项添加到id: aaa-2 ,对象将在下面:

const testArr = [
    { id: 'aaa', 
      children: [ 
           { id: 'aaa-1', 
             children: [
                { id: 'aaa-1-1' }   
             ] 
           }, 
           { id: 'aaa-2',
             children: [{ someNewKey: 'someNewValue' }]
           }

       ] 
    },
    { id: 'bbb' },
]

Is there a way to update nested level of object with specific key:value pair? 有没有办法用特定的键:值对更新嵌套的对象级别? I try to make function, but it's not working well 我尝试制作功能,但效果不佳

I tried // but it's not working properly when try to inner updates 我试过//但是在尝试内部更新时它无法正常工作

const updateDeep = (arr, id, push) => {
    return arr.map(el => {
        if (el.id === id) {
             return Object.assign({}, el, { children: push }) 
            }
        else {
            if (el.children) {
                return updateDeep(el.children, id, push)
            } else {
                return el
            }
        }
    })
}

Resolve it myself 自己解决它

function updateDeep(array, id, newObj) {
    array.some((o, i) => {
        var temp;
        if (o.id === id) {
            o.children = [...(o.children ? o.children : []), newObj]
        }
        if (temp = updateDeep(o.children || [], id, newObj)) {
            o.children = [...(o.children ? o.children : []), newObj]
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中将嵌套对象转换为对象数组 - How to convert nested object to array of objects in javascript 如何在javascript中更新嵌套对象数组 - how to update the nested object array in javascript 如何遍历嵌套的对象数组并更新 JavaScript 中的 object? - How do you iterate through a nested array of objects and update an object in JavaScript? 如何在对象数组的嵌套数组中获取数组对象javascript - How to get array object in nested array of array of objects javascript 如何更新 Javascript 中嵌套对象数组中的键/值对 - How to update a key/value pair in a nested array of objects in Javascript 如何转换数组中的对象,该数组已在javaScript中的嵌套对象中 - how to convert objects in array which is already in a nested object in javaScript 如何在 JavaScript 中将嵌套对象数组转换为具有键值对的对象 - How to convert an array of nested objects to an object with key, value pairs in JavaScript 如何通过在嵌套对象数组中传递 id 来获取对象 JavaScript - how to get object by passing id in an array of nested objects JavaScript 如何通过嵌套的 object 属性对 JavaScript 对象数组进行排序? - How to sort a JavaScript array of objects by nested object property? 如何在javascript中通过id更新数组中的嵌套对象属性? - How to update nested object property in array by id in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM