JavaScript array methods 1/3 - altering arrays
Daniel BudziΕski
Posted on August 15, 2021
The Array prototype in modern JavaScript contains many useful methods which every developer should know. However, some of them were introduced in the more recent ECMAScript. So if you don't keep up with the newest standards or you're just a beginner - it is a good time to learn something new. I'm also going to show you some tricks and trivia about these methods.
Before you read
If you're an absolute beginner before reading this article you can check my brief introduction to JavaScript arrays where I discussed this construct in short.
How to add new items to an array?
push()! One of the most common operations that we can do on arrays. It simply adds new elements to the array.
const array = ["π", "π¬", "π
"];
array.push("π");
console.dir(array) // Output: ["π", "π¬", "π
", "π"]
push() will automatically extend the size of the array and add our new element at the end. We can push() more than one element at once:
const array = ["π", "π¬", "π
"];
array.push("π", "π", "π");
console.dir(array) // Output: ["π", "π¬", "π
", "π", "π", "π"]
It's also worth noting that push() returns the new length of the array.
const array = ["π", "π¬", "π
"];
console.dir(array.push("π")); // Output: 4
console.dir(array); // Output: ["π", "π¬", "π
", "π"]
Sometimes we may need to manually extend an array and add elements at certain indexes.
const array = [];
array[2] = "π¬";
console.dir(array); // Output: [undefined, undefined, "π¬"]
array[0] = "π
";
array[1] = "π";
console.dir(array); // Output: ["π
", "π", "π¬"]
In this example, we create an empty array. The next line extends its size to n+1 and adds π¬ as the last value.
Thanks to the console.dir() in the third line you can see that there are some undefined values in this array. As I said in my brief introduction to JavaScript arrays, when you're extending the length of an array, JavaScript creates new elements with undefined values to meet the new size.
This method also works on existing arrays:
const array = ["π
", "π¬"];
array[4] = "π";
console.dir(array); // Output: ["π
", "π¬", undefined, undefined, "π"]
How to concat/merge arrays?
One of the possibilities is to use concat():
const array1 = ["π", "π¬", "π
"];
const array2 = ["π", "π"];
const result = array1.concat(array2);
console.dir(result); // Output: ["π", "π¬", "π
", "π", "π"]
It merges two or more arrays and returns the new array. Here's an example on three arrays:
const array1 = ["π", "π¬", "π
"];
const array2 = ["π", "π"];
const array3 = ["π", "π"];
const result = array1.concat(array2, array3);
console.dir(result); // Output: ["π", "π¬", "π
", "π", "π", "π", "π"]
But what if I want to merge one array INTO another array without the need to assign a third variable? ES2015 introduced a so-called destructuring assignment which in combination with push() can do it!
const array1 = ["π", "π¬", "π
"];
const array2 = ["π", "π"];
array1.push(...array2);
console.dir(array1); // Output: ["π", "π¬", "π
", "π", "π"]
Ta dam! Now we have all of the elements of the second array in our first array.
Using destructuring we can achieve a similar behavior to concat(). We just need to destruct the merged arrays into another array.
const array1 = ["π", "π¬", "π
"];
const array2 = ["π", "π"];
const array3 = ["π", "π"];
const result = [...array1, ...array2, ...array3];
console.dir(result);
Removing elements from the array
How to remove the last element from the array?
It's as simple as calling the pop() function on the array.
const array = ["π
", "π¬", "π"];
array.pop();
console.dir(array); // Output: ["π
", "π¬"]
pop() has also a useful property because it returns the removed element!
const array = ["π
", "π¬", "π"];
const lastElement = array.pop();
console.dir(lastElement); // Output: "π"
How to remove the first element from the array?
Here in handy comes shift(). Similar to pop() it also returns the element being removed.
const array = ["π
", "π¬", "π"];
const firstElement = array.shift();
console.dir(firstElement); // Output: "π
"
console.dir(array); // Output: ["π¬", "π"]
How to remove elements from the array on a specific index?
To remove a specific element we can use the delete operator.
const array = ["π
", "π¬", "π"];
delete array[1];
console.dir(array); // Output: ["π
", undefined, "π"]
It removes the element completely - the array now does not have an element with index 1. The interesting part of this is the fact, that we are still left with an array with a length of 3. If you want to leave the element and don't want it to have value just set it to undefined. Example:
const array = ["π
", "π¬", "π"];
delete array[1];
console.dir(array.length); // Output: 3
console.dir(1 in array); // Output: false
console.dir(array); // Output: ["π
", undefined, "π"]
array[1] = 'π¬';
array[1] = undefined;
console.dir(array.length); // Output: 3
console.dir(1 in array); // Output: true
console.dir(array); // Output: ["π
", undefined, "π"]
But what when we want to remove the element AND shorten the array? For this case, we can use splice().
const array = ["π
", "π¬", "π"];
array.splice(1, 1);
console.dir(array); // Output: ["π
", "π"]
The first argument of splice() is the startIndex, it sets the place where we want to start "cutting" our array. The second argument determines the length of the "cut". In 0ut case we only want to delete "π¬" so we just start "cutting" on index 1 and we want to remove just one element. Here is another example of removing more elements.
const array = ["π
", "π¬", "π", "π", "π", "π"];
array.splice(2, 3);
console.dir(array); // Output: ["π
", "π¬", "π"]
Now our "cut" has started on "π" and we wanted to remove three elements starting from that place.
With splice, we can also fill the gap of the removed elements by passing more arguments.
const array = ["π
", "π¬", "π", "π", "π", "π"];
array.splice(2, 2, "π", "π¦");
console.dir(array); // Output: ["π
", "π¬", "π", "π¦", "π", "π"]
or using destructuring, we can fill the gap with another array.
const array = ["π
", "π¬", "π", "π", "π", "π"];
const fillArray = ["π", "π¦"];
array.splice(2, 2, ...fillArray);
console.dir(array); // Output: ["π
", "π¬", "π", "π¦", "π", "π"]
Now let's compare all three methods and see the results!
const array = ["π
", "π¬", "π"];
delete array[1];
console.dir(array.length); // Output: 3
console.dir(1 in array); // Output: false
console.dir(array); // Output: ["π
", undefined, "π"]
array[1] = "π¬";
array[1] = undefined;
console.dir(array.length); // Output: 3
console.dir(1 in array); // Output: true
console.dir(array); // Output: ["π
", undefined, "π"]
array[1] = "π¬";
array.splice(1,1);
console.dir(array.length); // Output: 2
console.dir(1 in array); // Output: true
console.dir(array); // Output: ["π
", "π"]
Reassuming:
- delete removes the element but does not affect the array's size.
- setting an element to undefined does not remove it completely nor affects the array's size.
- splice() removes the element and affects the array's size.
Creating a string from an array - joining all elements into one string
Sometimes we need to create one string from all of the elements of the array, we can do it by using join().
const array = ["π
", "π¬", "π", "π", "π", "π"];
const result = array.join();
console.dir(result); // Output: "π
,π¬,π,π,π,π"
We can also specify the separator by passing it as the first argument.
const array = ["π
", "π¬", "π", "π", "π", "π"];
const result = array.join(' | ');
console.dir(result); // Output: "π
| π¬ | π | π | π | π"
Creating an array from a string - splitting strings
We can achieve this by calling split() on our string. I know that split() is not a part of the Array prototype, but I thought that I should mention it when I'm talking about its counterpart - join().
const string = "π
,π¬,π,π,π,π";
const result = string.split();
console.dir(result); // Output: ["π
", "π¬", "π", "π", "π", "π"]
The default separator for split() is "," but we can change it to whatever we want.
const string = "π
|π¬|π|π|π|π";
const result = string.split("|");
console.dir(result); // Output: ["π
", "π¬", "π", "π", "π", "π"]
How to reverse an array?
JavaScript also has a method for this - and (how surprising...) it's named... reverse().
const array = ["π
", "π¬", "π"];
const result = array.reverse();
console.dir(result); // Output: ["π", "π¬", "π
"]
But there is one problem with this method. It's mutating our original array. Well, it is only a problem if you want to preserve the original array.
const array = ["π
", "π¬", "π"];
const result = array.reverse();
console.dir(result); // Output: ["π", "π¬", "π
"]
console.dir(array); // Output: ["π", "π¬", "π
"]
What can we do to solve this issue? Well... just call concat() or slice() without any arguments.
const array = ["π
", "π¬", "π"];
const result = array.concat().reverse();
console.dir(result); // Output: ["π", "π¬", "π
"]
console.dir(array); // Output: ["π
", "π¬", "π"]
NOTE. This works because concat() and slice() are returning a copy of the array - not a reference. By using reverse() after them we're only altering the copy.
Or (a cleaner solution) using our best friend, the hero we all needed but didn't deserve him, destructuring assignment.
const array = ["π
", "π¬", "π"];
const result = [...array].reverse();
console.dir(result); // Output: ["π", "π¬", "π
"]
console.dir(array); // Output: ["π
", "π¬", "π"]
NOTE. Here we are just destructuring our array into another array, which is going to be reversed. So it's basically also calling reverse() on a copy.
We can also implement our own reverse function (this is only for absolute geeks).
Just kidding, we're not going to reinvent the wheel. I mean, you can, but... I'm too busy and we have to cover some more methods. However, these guys have time to do this, and you can check their thread for some funky solutions.
How to add new elements to the beginning of an array?
If JavaScript methods were people, push() and shift() would be a couple with a kid named unshift(). unshift() like push() will add new elements to the array but at the beginning.
const array = ["π
", "π¬", "π"];
array.unshift("π", "π");
console.dir(array); // Output: ["π", "π", "π
", "π¬", "π"]
And like push() it also accepts more than one element.
const array1 = ["π
", "π¬", "π"];
const array2 = ["π", "π"];
array1.unshift(...array1);
console.dir(array1); // Output: ["π", "π", "π
", "π¬", "π"]
Also, we can use destructuring to merge an array to the beginning of another.
Also similarly to push(), unshift() returns the new length of the array.
const array = ["π", "π¬", "π
"];
console.dir(array.unshift("π")); // Output: 4
console.dir(array); // Output: ["π", "π", "π¬", "π
"]
How to sort an array in JavaScript?
Sorting in JS is achieved with the sort() method. It utilizes an in-place algorithm so it doesn't copy the array, it alters the original.
const array = ["π
", "π¬", "π", "π", "π", "π"];
array.sort();
console.dir(array); // Output: ["π", "π
", "π", "π", "π", "π¬"]
If we want to keep the original we can do the same trick that we have done with reverse().
const array = ["π
", "π¬", "π", "π", "π", "π"];
const result = array.slice().sort();
console.dir(array); // Output: ["π
", "π¬", "π", "π", "π", "π"]
console.dir(result); // Output: ["π", "π
", "π", "π", "π", "π¬"]
const array = ["π
", "π¬", "π", "π", "π", "π"];
const result = [...array].sort();
console.dir(array); // Output: ["π
", "π¬", "π", "π", "π", "π"]
console.dir(result); // Output: ["π", "π
", "π", "π", "π", "π¬"]
NOTE. Emojis are just unicode characters and every emoji has it's unique code. For example π is U+1F404 and π is U+1F409. The algorithm is going to sort by this codes.
By default, this method sorts the elements using an ascending order - from lower to higher. If we want to sort in descending order we can write our own compare function (more on that in a moment) or just reverse() the sorted array (as it's more performant).
const array = ["π
", "π¬", "π", "π", "π", "π"];
array.sort().reverse();
console.dir(array); // Output: ["π¬", "π", "π", "π", "π
", "π"]
The default behavior of the sort() method causes also a very interesting problem, let's try to sort an array consisting of only numbers.
const array = [3, 45, 12, 1, 78, 369];
array.sort();
console.dir(array); // Output: [1, 12, 3, 369, 45, 78]
It provides us with one conclusion.
This is because by default sort is converting the elements to strings and comparing them in UTF-16. So when comparing words like "water" and "fire", "fire" comes first but when converting numbers to strings like 100 and 5 we end up with "100" coming before "5". To solve this we need to provide our own compare function as the first argument.
const array = [3, 45, 12, 1, 78, 369];
array.sort((first, second) => first - second);
console.dir(array); // Output: [1, 3, 12, 45, 78, 369]
Ah, much better.
NOTE. We use
first - second
rather thanfirst > second
because it's more performant.
The problem with sorting number arrays is not our only concern. If you're French, Polish, German, Czech, Spanish or a citizen of another country whose native language has some letters with diacritics and you want to compare some local strings... well your life is not easy then. Here's hows sort() is working with accent letters.
const array = ["turkuΔ podjadek", "konik polny", "komar", "mucha", "Δma"];
array.sort();
console.dir(array); // Output: ["komar", "konik polny", "mucha", "turkuΔ podjadek", "Δma"]
This example is using some Polish insect names. The words with an accent are just put at the end. For example "Δma" should be first but it's last. To fix this we need to provide our own compare function again.
const array = ["turkuΔ podjadek", "konik polny", "komar", "mucha", "Δma"];
array.sort((first, second) => first.localeCompare(second));
console.dir(array); // Output: ["Δma", "komar", "konik polny", "mucha", "turkuΔ podjadek"]
Now it's working. localeCompare() checks if the reference string comes after or before the string given.
How to get first/last/any n elements from an array?
slice() is the solution you're looking for. It accepts two arguments, the start index and the end index, both are optional, but when we provide neither of them - nothing happens. Here are some useful snippets.
Get the first 3 elements of an array
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(0, 3);
console.dir(result); // Output: ["π", "π
", "π"]
Get the last element of an array
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(-1);
console.dir(result); // Output: ["π¬"]
Get the second half of an array
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(array.length / 2);
console.dir(result); // Output: ["π", "π", "π¬"]
Get the first half of an array
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(0, array.length / 2);
console.dir(result); // Output: ["π", "π
", "π"]
Get elements after the fourth element
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(4);
console.dir(result); // Output: ["π", "π¬"]
Get a slice of the array
const array = ["π", "π
", "π", "π", "π", "π¬"];
const result = array.slice(2, 4);
console.dir(result); // Output: ["π", "π"]
As you can see, slice() can do many things.
How to flatten arrays?
Flattening means reducing the dimensions of an array. For example, if we got a two-dimensional array we can reduce it to only one dimension using flat().
const array = [["π", "π"], ["π
", "π"]];
const result = array.flat();
console.dir(result); // Output: ["π", "π", "π
", "π"]
Flattening doesn't affect the original array. It's copying its values.
By default flat() is going to flatten only one dimension. If you need to flatten a three (or more) dimensional array to just one dimension you have to provide the depth argument.
const array = [["π", "π"], ["π
", ["π", "π"]]];
const result = array.flat(2);
console.dir(result); // Output: ["π", "π", "π
", "π", "π"]
How to copy elements within an array?
Sometimes you want to copy an element from one position to another. For this, you can use copyWithin(). Like slice() this method has many possible use cases.
Copy first two elements to the last two elements
const array = ["π", "π", "π", "π¬", "π", "π
"];
array.copyWithin(-2);
console.dir(array); // Output: ["π", "π", "π", "π¬", "π", "π"]
Replacing one value with another
const array = ["π", "π", "π", "π¬", "π", "π
"];
array.copyWithin(2, 0, 1);
console.dir(array); // Output: ["π", "π", "π", "π¬", "π", "π
"]
Here we replaced the π on index 2, with the piece that goes from index 0 to index 1, which is the π. By changing the second argument to 2 we would also affect the π¬, basically inserting the π and π on the positions where π and π¬ were.
const array = ["π", "π", "π", "π¬", "π", "π
"];
array.copyWithin(2, 0, 2);
console.dir(array); // Output: ["π", "π", "π", "π", "π", "π
"]
For now - that's all. We've discussed all methods from the Array prototype that are used to alter arrays. This article series is going to be divided into 3 parts, the next part will deal with array iterators and looping through them, and the third would be about searching elements in arrays. If you appreciate my work consider visiting my blog and signing to my newsletter (I don't spam, you will be noticed only about my newest content :D).
Posted on August 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 24, 2024
November 22, 2024