1.delete 删除
list0: [
13, 14, 15, 17
],
list: [{
name: 'test01'
},
{
name: 'test02'
},
{
name: 'test03'
},
{
name: 'test03'
},
]
delete this.list0[0]
console.log(this.list0)
console.log(this.list0.length)

// 对象数组
delete this.list[0]
console.log(this.list)
console.log(this.list.length)

2.splice删除
this.list0.splice(1, 1)
console.log(this.list0)
console.log(this.list0.length)

this.list.splice(1, 1)
console.log(this.list)
console.log(this.list.length)

3.区别
delete: 只是被删除的元素变成了 empty ,数组长度不变
empty:数组的空元素empty表示空位, 它不是一种数据类型, 而是由于人为修改arr.length 或者写入时多写了逗号造成的。
undefined是一种数据类型, 在数组中表示这个位置的值未定义, 但它仍然指向某个内存地址, 这个内存地址指向的是undefined;
empty和undefined在操作数组时的主要区别在于:
使用数组的forEach()方法遍历时会自动忽略空位, 而使用for循环则会将empty转换为undefined并遍历.
splice: 该方法会改变原始数组
网友评论