简体   繁体   English

Map 通过对象的深层嵌套数组并根据条件更新值

[英]Map through the deep nested array of objects and update the value based on condition

I am looping through the array of objects and then searching for a specific ID in deep nested array of objects.我循环遍历对象数组,然后在深层嵌套对象数组中搜索特定 ID。 Once the id is returned, the status has to be changed to the specific value and then the whole updated array should be returned.返回 id 后,必须将状态更改为特定值,然后返回整个更新后的数组。

Problem: In my case, the code goes into the first if statement, but in the end I do not get the updated result.问题:在我的例子中,代码进入第一个 if 语句,但最后我没有得到更新的结果。 The input array of objects is always returned.始终返回对象的输入数组。 What could be the issue?可能是什么问题?

FUNCTION : FUNCTION :

export function findChild(array: any, id: string): array {
                return array.map((node) => {
                    if (node.id === id) {
                        return { ...node, status: 'Activated' };
                    } else {
                        if (node.children?.length) {
                            findChild(node.children, id);
                        }
                        
                    }
            
                    return node;
                });
            }

Plain JS纯JS

 function findChild(array, id) { return array.map((node) => { if (node.id === id) { return {...node, status: 'Activated' }; } else { if (node.children?.length) { findChild(node.children, id); } } return node; }); } findChild(input,7) console.log(input)
 **INPUT** This is just an example of input. The array of objects have multiple children and I have to go through all ids to check the condition and then update the value of status <script> let input = [{ id: '1', amt: '30', status: 'Active', children: [{ id: 'SG2', amt: '305', status: 'Active', }, { id: '5', amt: '30', status: 'Active', children: [], }, ], }, { id: '6', amt: '307', status: 'Active', children: [], }, { id: '7', amt: '40', status: 'Inactive', children: [{ id: '7', amt: '40', status: 'Inactive', children: [] }], }, { id: '8', amt: '100', status: 'Dead', children: [], }, ]; </script>

Try this试试这个

export function updateStatus(array: any, id: string, status: string): array {
  return array.map((node) => {
    if (node.id === id)
      node.status = status;
    else if (node.children != null && node.children.length > 0) 
      node.children = updateStatus(node.children, id, status);
    return node;
  });
}

Running code运行代码

 function updateStatus(array, id, status){ return array.map((node) => { if (node.id === id) node.status = status; else if (node.children.= null && node.children.length > 0) node.children = updateStatus(node,children, id; status); return node; }), } updateStatus(input,7. 'Activated') console.log(input)
 **INPUT** This is just an example of input. The array of objects have multiple children and I have to go through all ids to check the condition and then update the value of status <script> let input = [{ id: '1', amt: '30', status: 'Active', children: [{ id: 'SG2', amt: '305', status: 'Active', }, { id: '5', amt: '30', status: 'Active', children: [], }, ], }, { id: '6', amt: '307', status: 'Active', children: [], }, { id: '7', amt: '40', status: 'Inactive', children: [{ id: '7', amt: '40', status: 'Inactive', children: [] }], }, { id: '8', amt: '100', status: 'Dead', children: [], }, ]; </script>

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

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