Manipulating & Sorting Arrays
Arrays with JavaScript covered adding and removing items at the ends of an array. This page covers changing items in the middle, reversing and sorting the order, and finding where an item sits.
Changing the middle with splice
Section titled “Changing the middle with splice”splice(start, deleteCount, ...items) removes deleteCount items from index start, inserts any further arguments in their place, and returns the removed items as a new array:
const todo = ['wash car', 'pay rent', 'buy stamps', 'call plumber'];
const removed = todo.splice(1, 1);console.log(removed, todo);
todo.splice(1, 0, 'water plants', 'post parcel');console.log(todo);
todo.splice(0, 2, 'book dentist');console.log(todo);This prints:
[ 'pay rent' ] [ 'wash car', 'buy stamps', 'call plumber' ][ 'wash car', 'water plants', 'post parcel', 'buy stamps', 'call plumber'][ 'book dentist', 'post parcel', 'buy stamps', 'call plumber' ]A deleteCount of 0 inserts without removing anything, and removing two items while inserting one replaces them. Array slice and splice compares splice() with slice(), which copies part of an array without changing it.
Reversing and sorting
Section titled “Reversing and sorting”reverse() turns the array round, and sort() puts the items in order. Both change the array in place and return it:
const todo = ['pay rent', 'wash car', 'buy stamps'];
todo.reverse();console.log(todo);
todo.sort();console.log(todo);
const mixed = ['banana', 'Cherry', 'apple'];console.log(mixed.sort());This prints:
[ 'buy stamps', 'wash car', 'pay rent' ][ 'buy stamps', 'pay rent', 'wash car' ][ 'Cherry', 'apple', 'banana' ]With no arguments, sort() converts each item to a string and compares the strings by their UTF-16 code units. That gives alphabetical order only for text in a single case, which is why Cherry comes before apple: every capital letter sorts before every lower-case one. The same rule puts 100 before 9 in an array of numbers. Custom Array Sorts shows how to pass a compare function that fixes both problems.
Finding an item’s position
Section titled “Finding an item’s position”indexOf() returns the index of the first item that equals the value given, and lastIndexOf() returns the index of the last one. Both return -1 when there is no match, and both take an optional second argument, which is the index to start searching from:
const visits = ['Ana', 'Ben', 'Chloe', 'Ben', 'Dev'];
console.log(visits.indexOf('Ben'));console.log(visits.lastIndexOf('Ben'));console.log(visits.indexOf('Ben', 2));console.log(visits.indexOf('Ed'));This prints:
133-1Both methods compare with strict equality (===). Searching in Arrays covers includes() and the methods that take a callback, for searches that need more than an exact match.
Copying instead of changing
Section titled “Copying instead of changing”splice(), reverse() and sort() all change the original array, which can surprise other code that holds a reference to it. Since ES2023, each has a copying version that returns a new array and leaves the original alone:
const todo = ['pay rent', 'wash car', 'buy stamps'];
const sorted = todo.toSorted();const reversed = todo.toReversed();const trimmed = todo.toSpliced(0, 1);
console.log(sorted);console.log(reversed);console.log(trimmed);console.log(todo);This prints:
[ 'buy stamps', 'pay rent', 'wash car' ][ 'buy stamps', 'wash car', 'pay rent' ][ 'wash car', 'buy stamps' ][ 'pay rent', 'wash car', 'buy stamps' ]toSpliced() returns the new array rather than the removed items. All current browsers and Node 20 or later support these methods.
Steve Griffith covers this topic in his video Manipulating and Sorting Arrays in JavaScript.