踩坑记录

在遍历数组的时候操作数组

从 arr 数组中删去上去下标在 ast 数组中的元素
示例:
输入:
arr = [1, 2, 3, 4]
ast = [2,3]
输出:
arr 变为 [1, 2]

错误示例:hello

1
2
3
4
5
6
7
8
// arr表示待操作的数组
// ast中存储待删除元素的下标
arr=[1,2,3,4]
ast=[2,3]
ast.forEach((item)=>{
arr.splice(item,1)
})
// arr 变成了 [1, 2, 4]

正确示例

1
2
3
4
arr = [1, 2, 3, 4]
ast = [2, 3]
arr = arr.fliter((e,index)=>!ast.includes(index))
// arr 变成了 [1, 2]