如何记住一个方法是否改变了原始数组? [英] How to remember if a method mutates the original array?

查看:61
本文介绍了如何记住一个方法是否改变了原始数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know I can find a list of mutator methods on MDN, still, in practice I always forget if methods like push() or reverse() mutates the original array or creates a new one. Is there a logic to why certain methods are mutators and some are non-mutators, so I can easily remember?

推荐答案

也许一个有助于记住它们的方法是识别变异方法并将它们分组;只有少量.

Maybe a helpful way to remember them is to identify the mutating methods and group them; there's only a small amount.

从数组中添加/删除:

  • Array.prototype.fill() - 覆盖任何地方的元素
  • Array.prototype.pop() - 从头开始​​删除
  • Array.prototype.push() - 添加到开头
  • Array.prototype.shift() - 从末尾移除
  • Array.prototype.unshift() - 添加到末尾
  • Array.prototype.splice() - 在任何地方添加/删除
  • Array.prototype.fill() - overwrite elements anywhere
  • Array.prototype.pop() - remove from beginning
  • Array.prototype.push() - add to beginning
  • Array.prototype.shift() - remove from end
  • Array.prototype.unshift() - add to end
  • Array.prototype.splice() - add/remove anywhere

重新排列数组:

  • Array.prototype.flat() - 展平一个数组
  • Array.prototype.sort() - 使用排序函数重新排列元素
  • Array.prototype.reverse() - 反向元素
  • Array.prototype.flat() - flatten an array
  • Array.prototype.sort() - rearrange elements using a sorting function
  • Array.prototype.reverse() - reverse elements

古怪:

  • Array.prototype.copyWithin() - 老实说,我从未使用过这种方法
  • Array.prototype.copyWithin() - honestly, I've never used this method

变异数组方法列表 -

  • 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()

非变异数组方法列表 -

List of non-mutating array methods -

  • 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() - 按值查找值的索引
  • Array.prototype.join() - 使用分隔符将值组合成字符串
  • Array.prototype.keys() - 获取键的迭代器
  • Array.prototype.lastIndexOf() - 按值查找值的索引,从末尾开始
  • Array.prototype.map() - 使用映射函数创建一个新数组
  • Array.prototype.reduce() - 折叠每个值,产生一个新值
  • Array.prototype.reduceRight() - 从右边开始折叠每个值,产生一个新值
  • Array.prototype.slice() - 选择一个子数组
  • Array.prototype.some() - 检查某个值是否与函数匹配
  • Array.prototype.toLocaleString() - 数组的字符串表示,对值使用 toLocaleString
  • Array.prototype.toString() - 数组的字符串表示,对值使用 toString
  • Array.prototype.values() - 获取值的迭代器
  • Array.prototype[@@iterator]() - 获取默认迭代器
  • Array.from() - create an array from an iterable
  • Array.isArray() - check if a variable is an array
  • Array.of() - create an array; function version of []
  • Array.prototype.concat() - combine several arrays into a new single array
  • Array.prototype.entries() - get iterator of key/value pairs
  • Array.prototype.every() - check if every value matches a function
  • Array.prototype.filter() - create an array of values matching a filter
  • Array.prototype.find() - find a value using a function
  • Array.prototype.findIndex() - find the index of a value using a function
  • Array.prototype.flatMap() - create an new array using a mapping function
  • Array.prototype.forEach() - run a side effect for each value
  • Array.prototype.includes() - check if the array includes a value
  • 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

这篇关于如何记住一个方法是否改变了原始数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆