The Methods to Update an Array: Cheat Sheet
ECMAScript 2023 (ES2023 or ES14) introduces a fresh set of array instance methods to enhance the way arrays can be modified.
ECMAScript 2023 (ES2023 or ES14) introduces a fresh set of array instance methods to enhance the way arrays can be modified.
In total four new methods are added, toSorted() (vs sort()), toReversed() (vs reverse()), toSpliced() (vs splice()), and with (vs bracket notation).
These methods use immutable operations on arrays, making it easier and more predictable to work with data without directly modifying the original array.
If you're use TypeScript, these methods are available in version 5.2.
Let's dive into these new methods, and at the same time refresh our knowledge for the existing methods.
| Description | Mutable Version | Immutable Version |
|---|---|---|
| Sorts the array | sort(compareFn) |
toSorted(compareFn) - NEW |
| Reverses the array | reverse() |
toReversed() - NEW |
| Change an element in the array | bracket notation (.[index] = e1) |
with(index, value) - NEW |
| Changes the contents of the array | splice(start, deleteCount, items?) |
toSpliced(start, deleteCount, items?) - NEW |
| Removes the last element | pop() |
slice(0, -1) |
| Removes the first element | shift() |
slice(1) |
| Adds element(s) to the end of the array | push(e1, e2) |
concat([e1, e2]) |
| Adds element(s) to the start of the array | unshift(e1, e2) |
toSpliced(0, 0, e1, e2) |
| Changes all elements within a range to a static value | fill(value, start, end?) |
❌ Not available |
| Shallow copies part of this array to another location in the same array | copyWithin(target, start, end?) |
❌ Not available |
/*** Sorts the array - by id* sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort* toSorted: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted*/update,(persons)=>.sort(a,b)=>.-.;update,(persons)=>.toSorted(a,b)=>.-.;/*** Reverses the array* reverse: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse* toReversed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed*/update,(persons)=>.reverse;update,(persons)=>.toReversed;/*** Changes an element in the array* bracket notation* toSpliced: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with*/update,(persons)=>{1={:9,:};return;};update,(persons)=>.with1,{:9,:};// 🎉 New in ES2023 (and TS 5.2)/*** Changes the contents of the array - remove person at index 2 and 3, add new person at index 0* splice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice* toSpliced: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed*/update,(persons)=>{.log.splice2,2;// returns the deleted item.log.splice0,0,{:9,:};// returns the deleted item (empty in this case)return;};update,(persons)=>.toSpliced2,2.toSpliced0,0,{:9,:},;// 🎉 New in ES2023 (and TS 5.2)/*** Removes the last element* pop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop* slice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice*/update,(persons)=>{.log.pop;// returns the deleted itemreturn;};update,(persons)=>.slice0,-1;/*** Removes the first element* shift: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift* slice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice*/update,(persons)=>{.log.shift;// returns the deleted itemreturn;};update,(persons)=>.slice1;/*** Adds element(s) to the end of the array* push: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push* concat: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat*/update,(persons)=>{.log.push{:9,:};// returns the new lengthreturn;};update,(persons)=>.concat{:9,:};/*** Adds element(s) to the start of the array* unshift: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift* toSpliced: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced*/update,(persons)=>{.log.unshift{:9,:},{:10,:};// returns the new lengthreturn;};update,(persons)=>.toSpliced0,0,{:9,:},{:10,:},;/*** Changes all elements within a range to a static value* fill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill* ❌: has no immutable version*/update,(persons)=>.fill{:9,:},2,4;/*** Shallow copies part of this array to another location in the same array* copyWithin: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin* ❌: has no immutable version*/update,(persons)=>.copyWithin1,2,3;// Helpersfunctionupdate(description:string,updater:(original:Person)=>Person){const:Person=initPersons;const=updater;.log${},{,,};}functioninitPersons():Person{return{:2,:},{:1,:},{:5,:},{:4,:},{:3,:},;}typePerson={id:number;name:string};
For additional details, check out the MDN documentation. Besides the new mutable methods, the ES2023 also added the findLast, and findLastIndex methods for an easy lookup of the last element in an array.
A runnable example can be found in the following the TypeScript Playground.
Feel free to update this developer bit on GitHub, thanks in advance!