二维数组删除子元素

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)
                            }

                        })
                    }
                }

            })
        }
阅读 1.6k
2 个回答

因为你用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--
                }
            }
        }
    }
})

数组删除是相对比较麻烦的操作,其实你可以换一个思路,根据前置条件逐渐生成navbarData

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏