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?
To sort an array, without mutating the original array:Call the slice() method on the array to get a copy. Call the sort() method on the copied array. The sort method will sort the copied array, without mutating the original.
The pop() method is a mutating method. It changes the length and the content of this . In case you want the value of this to be the same, but return a new array with the last element removed, you can use arr.slice(0, -1) instead.
prototype. push allows us to push elements to the end of an array. This method does not return a new copy, rather mutates the original array by adding a new element and returns the new length property of the object upon which the method was called.
Maybe a helpful way to remember them is to identify the mutating methods and group them; there's only a small amount.
Add/remove from array:
Array.prototype.fill() - overwrite elements anywhereArray.prototype.pop() - remove from rightArray.prototype.push() - add to rightArray.prototype.shift() - remove from leftArray.prototype.unshift() - add to leftArray.prototype.splice() - add/remove anywhereRearrange arrays:
Array.prototype.sort() - rearrange elements using a sorting functionArray.prototype.reverse() - reverse elementsOddball:
Array.prototype.copyWithin() - honestly, I've never used this methodList of mutating array methods
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() - 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.flat() - flatten a nested arrayArray.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, uses toLocaleString on valuesArray.prototype.toString() - string representation of the array, uses toString on valuesArray.prototype.values() - get iterator of valuesArray.prototype[@@iterator]() - get default iteratorIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With