js循环splice删除可能会遇到的坑

## 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值