如何记住一个方法是否改变了原始数组? [英] How to remember if a method mutates the original array?
本文介绍了如何记住一个方法是否改变了原始数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以在 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 anywhereArray.prototype.pop()
- remove from beginningArray.prototype.push()
- add to beginningArray.prototype.shift()
- remove from endArray.prototype.unshift()
- add to endArray.prototype.splice()
- add/remove anywhere
重新排列数组:
Array.prototype.flat()
- 展平一个数组Array.prototype.sort()
- 使用排序函数重新排列元素Array.prototype.reverse()
- 反向元素
Array.prototype.flat()
- flatten an arrayArray.prototype.sort()
- rearrange elements using a sorting functionArray.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 iterableArray.isArray()
- check if a variable is an arrayArray.of()
- create an array; function version of[]
Array.prototype.concat()
- combine several arrays into a new single arrayArray.prototype.entries()
- get iterator of key/value pairsArray.prototype.every()
- check if every value matches a functionArray.prototype.filter()
- create an array of values matching a filterArray.prototype.find()
- find a value using a functionArray.prototype.findIndex()
- find the index of a value using a functionArray.prototype.flatMap()
- create an new array using a mapping functionArray.prototype.forEach()
- run a side effect for each valueArray.prototype.includes()
- check if the array includes a valueArray.prototype.indexOf()
- find the index of a value by valueArray.prototype.join()
- combine values into a string using a separatorArray.prototype.keys()
- get iterator of keysArray.prototype.lastIndexOf()
- find the index of a value by value, starting at the endArray.prototype.map()
- create a new array using a mapping functionArray.prototype.reduce()
- fold over each value, producing a new valueArray.prototype.reduceRight()
- fold over each value, starting from the right, producing a new valueArray.prototype.slice()
- select a subarrayArray.prototype.some()
- check if some value matches a functionArray.prototype.toLocaleString()
- string representation of the array, usestoLocaleString
on valuesArray.prototype.toString()
- string representation of the array, usestoString
on valuesArray.prototype.values()
- get iterator of valuesArray.prototype[@@iterator]()
- get default iterator
这篇关于如何记住一个方法是否改变了原始数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文