Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

二维数组删除子元素

var quanxian_t=[{name: "书本管理", power: "book_manage", status: "1", cate1: "ucbase", cate2: "基础数据"}
 {name: "章节/知识点管理", power: "cate_know_manage", status: "1", cate1: "ucbase", cate2: "基础数据"}
{name: "专家题库查看", power: "pro_view", status: "0", cate1: "yzy", cate2: "云作业"}
 {name: "专家题库管理", power: "pro_manage", status: "1", cate1: "yzy", cate2: "云作业"}
{name: "考试阅卷", power: "exam_manage", status: "1", cate1: "yzy", cate2: "云作业"}
 {name: "组卷管理", power: "exam_manage_zjgl", status: "0", cate1: "yzy", cate2: "云作业"}
 {name: "有效生设置", power: "focus_stu_manage", status: "0", cate1: "yzy", cate2: "云作业"}
{name: "组卷审核", power: "exam_check", status: "0", cate1: "yzy", cate2: "云作业"}]


 var navbarData = [{
            id: "qqyxt",
            label: "亲亲云学堂",
            arr: []
        },
        {
            id: "school_task",
            label: "作业首页",
            arr: []
        },
        {
            id: "issue_combine",
            label: "出题组卷",
            arr: [{
                    id: "issue_combine_chapter",
                    label: "章节出题"
                },
                {
                    id: "issue_combine_knowledge",
                    label: "知识点出题"
                },
                {
                    id: "issue_combine_intellect",
                    label: "智能出题"
                }
            ]
        },
        {
            id: "exam_mark",
            label: "考试阅卷",
            arr: [{
                    id: "exam_mark_online",
                    label: "在线考试"
                },
                {
                    id: "exam_mark_manage",
                    label: "组卷管理"
                },
                {
                    id: "line_down_test",
                    label: "线下考试"
                }
            ]
        },
        {
            id: "paper_library",
            label: "试卷库",
            arr: []
        },
        {
            id: "expert_task",
            label: "专家题库",
            arr: [{
                    id: "expert_task_chapter",
                    label: "章节出题"
                },
                {
                    id: "expert_task_knowledge",
                    label: "知识点出题"
                },
                {
                    id: "expert_task_intellect",
                    label: "智能出题"
                }
            ]
        },
        {
            id: "vip_task",
            label: "VIP题库",
            arr: [{
                    id: "vip_task_chapter",
                    label: "章节出题"
                },
                {
                    id: "vip_task_knowledge",
                    label: "知识点出题"
                }
            ]
        },
        {
            id: "prop_requirement",
            label: "命题要求",
            arr: []
        }
    ];

//需求 如果组卷管理里面的status==0 把navbarData里面的 {

                id: "exam_mark_manage",
                label: "组卷管理"
            }删除,请教大神报错的原因

 if (quanxian_t.length) {
            let navbarData = this.navbarData
            $.each(quanxian_t, function(i, v) {
                if (v.power == 'exam_manage_zjgl' && v.status == 0) {
                    for (let i in navbarData) {
                        let children = navbarData[i].arr
                        $.each(children, function(ii, vv) {
                            if (vv.id === 'exam_mark_manage') {
                                children.splice(Number(ii), 1)
                            }

                        })
                    }
                }

            })
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

因为你用splice id等于exam_mark_manage,这时children为

[{
   id: "exam_mark_online",
   label: "在线考试"
},
{
   id: "line_down_test",
   label: "线下考试"
}]

然后接着循环到第三个的时候,vv就是undefined了,所以报id of undefined
可以这么修改

 $.each(quanxian_t, function(i, v) {
    if (v.power == 'exam_manage_zjgl' && v.status == 0) {
        for (let i in navbarData) {
            let children = navbarData[i].arr
            for(let i=0;i<children.length;i++) {
                if (children[i].id === 'exam_mark_manage') {
                    children.splice(i, 1)
                    i--
                }
            }
        }
    }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...