如何记住一个方法是否改变了原始数组?

V. *_*ang 5 javascript methods

我知道我可以在 MDN 上找到一组 mutator 方法,但实际上我总是忘记 push() 或 reverse() 之类的方法是否会改变原始数组或创建一个新数组。为什么某些方法是变异器而有些是非变异器,所以我很容易记住,这是否有逻辑?

Tha*_*you 8

也许记住它们的一种有用方法是识别变异方法并将它们分组;只有少量。

从数组中添加/删除:

  • Array.prototype.fill() - 在任何地方覆盖元素
  • Array.prototype.pop() - 从头删除
  • Array.prototype.push() - 添加到开头
  • Array.prototype.shift() - 从末端移除
  • Array.prototype.unshift() - 添加到结尾
  • Array.prototype.splice() - 在任何地方添加/删除

重新排列数组:

  • Array.prototype.flat() - 展平一个数组
  • Array.prototype.sort() - 使用排序功能重新排列元素
  • Array.prototype.reverse() - 反向元素

古怪:

  • Array.prototype.copyWithin() - 老实说,我从未使用过这种方法

变异数组方法列表 -

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

非变异数组方法列表 -

  • Array.from() - 从一个可迭代对象创建一个数组
  • Array.isArray() - 检查变量是否为数组
  • Array.of()- 创建一个数组;功能版本[]
  • Array.prototype.concat() - 将多个数组组合成一个新的单个数组
  • Array.prototype.entries() - 获取键/值对的迭代器
  • Array.prototype.every() - 检查每个值是否与函数匹配
  • Array.prototype.filter() - 创建一个匹配过滤器的值数组
  • Array.prototype.find() - 使用函数查找值
  • Array.prototype.findIndex() - 使用函数查找值的索引
  • Array.prototype.flatMap() - 使用映射函数创建一个新数组
  • Array.prototype.forEach() - 为每个值运行一个副作用
  • Array.prototype.includes() - 检查数组是否包含一个值
  • Array.prototype.indexOf() - find the index of a value by value
  • Array.prototype.join() - combine values into a string using a separator
  • Array.prototype.keys() - get iterator of keys
  • Array.prototype.lastIndexOf() - find the index of a value by value, starting at the end
  • Array.prototype.map() - create a new array using a mapping function
  • Array.prototype.reduce() - fold over each value, producing a new value
  • Array.prototype.reduceRight() - fold over each value, starting from the right, producing a new value
  • Array.prototype.slice() - select a subarray
  • Array.prototype.some() - check if some value matches a function
  • Array.prototype.toLocaleString() - string representation of the array, uses toLocaleString on values
  • Array.prototype.toString() - string representation of the array, uses toString on values
  • Array.prototype.values() - get iterator of values
  • Array.prototype[@@iterator]() - get default iterator