![](/img/trans.png)
[英]How can I convert a deep nested array into a deep nested object, using recursion?
[英]How to loop with recursion in deep nested array ? JS
我需要遍歷深層 arrays,並更改一些值
arrays 示例 ->
let arr =
[{
id: 1 ,
name : "Test 1 " ,
title : "title 1 " ,
hasChild : false,
children :
[
{ id: 2 , name : "test 2" , title : 'title 2', hasChild true, children: [] },
{ id: 3 , name : "test 3" , title : 'title 3', hasChild false, children: [] },
{ id: 4 , name : "test 4" , title : 'title 4', hasChild false , children :
[
id: 1 ,
name : "Test 1 " ,
title : "title 1 " ,
hasChild : true,
]},
]
},
]
循環后,我需要有一個相同的列表,但在每個將hasChild設置為 false 的數組中。
我正在嘗試:
arr.children.forEach(item => item.children.hasChild = false)
但我還需要循環思考兒童 - >兒童列表,並設置兒童兒童兒童假......
這里可能不止這個兒童系列
我需要為每個孩子設置 false 。
const setHasChildToFalse = (arr) => {
arr.forEach(item => {
item.hasChild = false;
if (item.children.length > 0) {
setHasChildToFalse(item.children);
}
});
}
console.log(arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.