js遍历树形数据-children
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>递归遍历children</title></head><body></body><script>let arr = [{name: 'test1'}
·
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>递归遍历children</title>
</head>
<body>
</body>
<script>
let arr = [{
name: 'test1'
}, {
name: 'test2',
children: [{
name: 'test2-1'
}]
}, {
name: 'test3',
children: [{
name: 'test1-1',
children: [{
name: 'test2-1-1'
}]
}]
}]
calleArr(arr);
function calleArr(array) {
for (i in array) {
var data = array[i];
if (data.children) {
console.log("带children的元素");
calleArr(data.children) //自己调用自己
} else {
console.log("children为空的元素");
}
}
}
</script>
</html>
更多推荐
已为社区贡献7条内容
所有评论(0)