首页  >  问答  >  正文

javascript - js 多维数组的问题

[
    {
        "id": 1,
        "name": "sys",
        "title": "系统设置",
        "type": 1,
        "status": 1,
        "condition": "",
        "pid": 0,
        "level": 0,
        "sort": 7,
        "icon": "fa-gear",
        "children": [
            {
                "id": 11,
                "name": "conf/lst",
                "title": "配置列表",
                "type": 1,
                "status": 1,
                "condition": "",
                "pid": 1,
                "level": 1,
                "sort": 50,
                "icon": null,
                "children": [
                    {
                        "id": 12,
                        "name": "conf/add",
                        "title": "添加配置",
                        "type": 1,
                        "status": 1,
                        "condition": "",
                        "pid": 11,
                        "level": 2,
                        "sort": 50,
                        "icon": null,
                        "children": []
                    },
                    {
                        "id": 13,
                        "name": "conf/del",
                        "title": "配置删除",
                        "type": 1,
                        "status": 1,
                        "condition": "",
                        "pid": 11,
                        "level": 2,
                        "sort": 50,
                        "icon": null,
                        "children": []
                    },
                    {
                        "id": 14,
                        "name": "conf/edit",
                        "title": "配置编辑",
                        "type": 1,
                        "status": 1,
                        "condition": "",
                        "pid": 11,
                        "level": 2,
                        "sort": 50,
                        "icon": null,
                        "children": []
                    }
                ]
            },
            {
                "id": 9,
                "name": "conf/conf",
                "title": "配置项",
                "type": 1,
                "status": 1,
                "condition": "",
                "pid": 1,
                "level": 1,
                "sort": 50,
                "icon": null,
                "children": []
            }
        ]
    },
    {
        "id": 15,
        "name": "admin",
        "title": "管理员",
        "type": 1,
        "status": 1,
        "condition": "",
        "pid": 0,
        "level": 0,
        "sort": 50,
        "icon": "fa-user",
        "children": [
            {
                "id": 16,
                "name": "admin/lst",
                "title": "管理员列表",
                "type": 1,
                "status": 1,
                "condition": "",
                "pid": 15,
                "level": 1,
                "sort": 50,
                "icon": null,
            },
            {
                "id": 27,
                "name": "authrule/lst",
                "title": "权限列表",
                "type": 1,
                "status": 1,
                "condition": "",
                "pid": 15,
                "level": 1,
                "sort": 50,
                "icon": null,
            },
            {
                "id": 30,
                "name": "authgroup/lst",
                "title": "用户组",
                "type": 1,
                "status": 1,
                "condition": "",
                "pid": 15,
                "level": 1,
                "sort": 50,
                "icon": null,
            }
        ]
    }
]

上面的json是多维数组,我想用js for循环把children下面的数组输出,但不知道为什么输出不了,也没报错.

$.ajax({
    type: "get",
    url: "/admin/index/menu",
    async: true,
    dataType: 'json',
    success: function(res) {
        for(var i = 0; i < res.length; i++) {
            console.log(res[i].children);    //这个能输出
            for (var a=0;a<res[i].children;a++) {
                console.log(res[i].children[a]);    //这个不能输出,也没有报错
            }
        }
    }
})

请问是哪里错了?

滿天的星座滿天的星座2683 天前729

全部回复(4)我来回复

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:41:52

    雷雷

    回复
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:41:52

    a<res[i].children -> a<res[i].children.length

    回复
    0
  • 怪我咯

    怪我咯2017-07-05 10:41:52

    虽然来晚了,但是我觉得还是可以补充一下

    一般我个人比较喜欢使用 foreach 遍历,在 JS 里是(以此例中的代码为例)

    res.forEach(r => {
        r.children.forEach(c => {
            // do something
        });
    });

    上面用了es6的箭头函数,如果要在 es5 中写,直接换成 function 表达式就好

    回复
    0
  • 学习ing

    学习ing2017-07-05 10:41:52

    这里应该是要做个递归,推荐了解下递归知识
    递归遍历节点

    回复
    0
  • 取消回复