js 遍历tree的一个例子

小例子蕴含着到力量,我希望自己有改变自己生活的能力,自己有主宰自己命运的能力...

 备注:这个例子有问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    
</head>
<body>
    <div>
        <h1 onclick="test()">test</h1>
    </div>
    <script>

        var arrs = [];
    
        function test(){
           var data = [
               {
                   name: '中国',
                   children: [
                       {
                           name: '教第三节课',
    
                       },
                       {
                           name: '教呼呼',
                           children: [
                               {
                                   name: '大一',
                                   children: [
                                       {
                                           name: '课程1',
                                           children: [
                                               {
                                                   name: '1231'
                                               },
                                               {
                                                   name: '121'
                                               }
                                           ]
                                       },
                                       {
                                           name: '课程2',
                                           children: [
                                               {
                                                   name: '1232'
                                               },
                                               {
                                                   name: '122'
                                               }
                                           ]
                                       },
                                       {
                                           name: '课程3',
                                           children: [
                                               {
                                                   name: '1233'
                                               },
                                               {
                                                   name: '123'
                                               }
                                           ]
                                       },
    
                                   ]
                               }
                           ]
                       },
                       {
                           name: '活动',
                           children:null
                       }
                   ]
               }
           ];
            getArray(data,'活动');
            for(var i in undefined){
                alert(123);
            }
        }
    
        function getArray(data,name)
        {
            for (var i in data) {
                
                if (data[i].name == name) {
                    break;
                } else {
                    getArray(data[i].children, name);
                }
            }
        }
    
    </script>
    
</body>
</html>

 

posted @ 2018-11-24 00:11  Fanyee  阅读(401)  评论(0编辑  收藏  举报