您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在JavaScript中删除数组元素-Delete与Splice

在JavaScript中删除数组元素-Delete与Splice

delete删除对象属性,但不会为数组重新索引或更新其长度。这使得它看起来好像是未定义的:

> myArray = ['a', 'b', 'c', 'd']
  ["a", "b", "c", "d"]
> delete myArray[0]
  true
> myArray[0]
  undefined

请注意,实际上并没有将其设置为value 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"]
SQL 2022/1/1 18:13:32 有1000人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶