簡體   English   中英

Map 通過對象的深層嵌套數組並根據條件更新值

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

我循環遍歷對象數組,然后在深層嵌套對象數組中搜索特定 ID。 返回 id 后,必須將狀態更改為特定值,然后返回整個更新后的數組。

問題:在我的例子中,代碼進入第一個 if 語句,但最后我沒有得到更新的結果。 始終返回對象的輸入數組。 可能是什么問題?

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

純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>

試試這個

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

運行代碼

 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