//json对象
const json = {
code: 200,
data: [
{
children: [
{ id: 85, path: [0, 1, 85], pid: 1 },
{ id: 86, path: [0, 1, 86], pid: 1 },
{ id: 87, path: [0, 1, 87], pid: 1 },
{ id: 153, path: [0, 1, 153], pid: 1 },
{ id: 154, path: [0, 1, 154], pid: 1 },
{ id: 179, path: [0, 1, 179], pid: 1 },
{ id: 181, path: [0, 1, 181], pid: 1 },
],
id: 1,
path: [0, 1],
pid: 0,
},
{
children: [
{
children: [{ id: 142, path: [0, 8, 141, 142], pid: 141 }],
id: 141,
path: [0, 8, 141],
pid: 8,
},
],
id: 8,
path: [0, 8],
pid: 0,
},
],
};
let parentkey = [];
function getkey(element) {
element.forEach((el) => {
if (el.children) {
parentkey.push(el.id);
return getkey(el.children);//递归调用 传入子数组
} else {
return;
}
});
}
//传入一个数组
getkey(json.data);
console.log(parentkey)
js 递归数组
最新推荐文章于 2024-02-29 14:12:46 发布