delete将删除对象属性,但不会重新索引数组或更新其长度。 这使它看起来好像是未定义的:
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined请注意,它实际上未设置为undefined值,而是从数组中删除该属性,使其显示为undefined。 通过Chrome开发工具记录打印empty来明确区分。
> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]myArray.splice(start, deleteCount)实际上删除元素,重新索引数组,并更改其长度。
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。