<!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>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐