## splice删除一级
const list = [
{"className": "花花5555", "gradeName": "毕业班"},
{"className": "中ei班", "gradeName": "毕业班"},
{"className": "花花班5", "gradeName": "小班"},
{"className": "花花", "gradeName": "大班"},
{"className": "花花班", "gradeName": "中班"},
{"className": "苹果班", "gradeName": "大班"},
{"className": "呃呃呃", "gradeName": "中班"},
{"className": "中二班", "gradeName": "毕业班"},
{"className": "大一班", "gradeName": "大班"},
{"className": "大一班1", "gradeName": "毕业班"},
{"className": "中一班", "gradeName": "毕业班"}
]
for (let i = 0; i < list.length; i++) {
if (list[i].gradeName === '毕业班') {
list.splice(i, 1)
}
}
console.log(JSON.stringify(list))
打印结果:[{"className":"中ei班","gradeName":"毕业班"},
{"className":"花花班5","gradeName":"小班"},
{"className":"花花","gradeName":"大班"},
{"className":"花花班","gradeName":"中班"},
{"className":"苹果班","gradeName":"大班"},
{"className":"呃呃呃","gradeName":"中班"},
{"className":"大一班","gradeName":"大班"},
{"className":"中一班","gradeName":"毕业班"}
]
for (let i = 0; i < list.length; i++) {
if (list[i].gradeName === '毕业班') {
list.splice(i, 1)
i = i - 1 //满足条件时不要跳过下一个元素
}
}
console.log(JSON.stringify(list))
打印结果:
[{"className":"花花班5","gradeName":"小班"},{"className":"花花","gradeName":"大班"},{"className":"花花班","gradeName":"中班"},{"className":"苹果班","gradeName":"大班"},{"className":"呃呃呃","gradeName":"中班"},{"className":"大一班","gradeName":"大班"}]
gradeName为‘毕业班’的都被过滤掉了!
循环splice删除一级和二级
let filrer = ["1", "1_1", "1_2", "1_3", "1_4", "1_5", "1_6", "1_7", "2_1", "2_3", "2_4", "2_6", "3_1", "3_2", "5",
"5_1", "5_2", "5_3", "5_4", "3_6", "3_7", "2", "3"
];
let treeData = [{
key: '1',
title: '重要报文',
children: [{
key: '1_1',
title: '语音',
},
{
key: '1_2',
title: '文本',
},
{
key: '1_3',
title: '传真',
},
{
key: '1_4',
title: '邮件',
},
{
key: '1_5',
title: '网页',
},
{
key: '1_6',
title: '图片',
},
{
key: '1_7',
title: '视频',
}
],
},
{
key: '2',
title: '已阅报文',
children: [{
key: '2_1',
title: '语音',
},
{
key: '2_2',
title: '文本',
},
{
key: '2_3',
title: '传真',
},
{
key: '2_4',
title: '邮件',
},
{
key: '2_5',
title: '网页',
},
{
key: '2_6',
title: '图片',
},
{
key: '2_7',
title: '视频',
}
]
},
{
key: '3',
title: '未阅报文',
children: [{
key: '3_1',
title: '语音',
},
{
key: '3_2',
title: '文本',
},
{
key: '3_3',
title: '传真',
},
{
key: '3_4',
title: '邮件',
},
{
key: '3_5',
title: '网页',
},
{
key: '3_6',
title: '图片',
},
{
key: '3_7',
title: '视频',
}
]
},
{
key: '4',
title: '已抄报文',
children: [{
key: '4_1',
title: '语音',
},
{
key: '4_2',
title: '文本',
},
{
key: '4_3',
title: '传真',
},
{
key: '4_4',
title: '邮件',
},
{
key: '4_5',
title: '网页',
},
{
key: '4_6',
title: '图片',
},
{
key: '4_7',
title: '视频',
}
]
},
{
key: '5',
title: '目标报文',
children: [{
key: '5_1',
title: '重要报文',
},
{
key: '5_2',
title: '黑名单',
},
{
key: '5_3',
title: '白名单',
},
{
key: '5_4',
title: '重要号码',
}
],
},
];
for (let i = 0; i < treeData.length; i++) {
// console.log(filrer.indexOf(treeData[i].key))
if (filrer.indexOf(treeData[i].key)=='-1') {
treeData.splice(i, 1)
i = i - 1 //满足条件时不要跳过下一个元素
}
if(treeData[i].children.length>0){
for (let j = 0; j < treeData[i].children.length; j++) {
if (filrer.indexOf(treeData[i].children[j].key)=='-1') {
treeData[i].children.splice(j, 1)
j = j - 1 //满足条件时不要跳过下一个元素
}
}
}
}
console.log(treeData)