javascript:更改应用到的数组的数组方法列表

javascript: List of array methods that change the array they were applied to

本文关键字:数组 方法 列表 应用 javascript      更新时间:2023-09-26

我经常在脑子里想这个问题,而且我不能很快用谷歌搜索出来。这就是为什么我在这里提出这个问题。

每次我调用像concatsortslice这样的方法时,我都会问自己:它是创建并返回一个新数组,还是只是修改并返回它所应用的数组?

Q:能否列出更改原始数组的方法与构造并返回新数组的方法?

poppush这样的方法不在游戏中,它们的作用很明显。

数组实例上更改数组中值或值个数的方法的完整列表如下:

  • Array.prototype.push
  • Array.prototype.pop
  • Array.prototype.shift
  • Array.prototype.unshift
  • Array.prototype.splice
  • Array.prototype.reverse
  • Array.prototype.sort
  • Array.prototype.fill
  • Array.prototype.copyWithin

另外,更改length属性可以从数组中删除元素(如果减少)或向数组中添加未定义的项(如果增加)。

最后,用像'0''1''3000'等正整数字符串的键更改数组的属性将更改这些索引处的值,并可能增加数组的长度。