示例:
demo.js:
const arr = [1, 2, 3, 4, 5, 6]
// 假设要删除数组中3个这个元素
// 先查找3这个元素的索引
const index = arr.findIndex((item) => item === 3)
if (index > -1) {
// 第一个参数表示从哪个索引开始,第二个参数表示删除几个元素
arr.splice(index, 1)
}
// 打印变化以后的数组
console.log(arr)
运行结果: