Last updated: Mar 1, 2024
Reading timeยท4 min
Array.map()
Array.reduce()
for
loopArray.fill()
To update all elements in an array:
Array.forEach()
method to iterate over the array.const arr = ['bobby', 'hadz', 'com']; arr.forEach((element, index) => { arr[index] = element + index; }); console.log(arr); // ๐๏ธ [ 'bobby0', 'hadz1', 'com2' ]
The function we passed to the Array.forEach() method gets called with each element in the array.
The function gets passed 3 arguments on each iteration:
You can use this approach to modify all array elements using any value.
const arr = [1, 2, 3, 4, 5]; arr.forEach((element, index) => { arr[index] = element + 10; }); console.log(arr); // ๐๏ธ [ 11, 12, 13, 14, 15 ]
On each iteration, we access the array element at the current index and update
its value by adding 10
to it.
We could have achieved the same result by using a plain for
loop. However,
for
loops are a bit more verbose and indirect.
Array.map()
This is a three-step process:
Array.map()
method to iterate over the array.Array.map()
method gives us access to the current element and its
index.const arr = ['bobby', 'hadz', 'com']; const newArray = arr.map((element, index) => { return element + index; }); // ๐๏ธ [ 'bobby0', 'hadz1', 'com2' ] console.log(newArray);
We used the map()
method in a similar way to how we used the forEach()
method.
The function we passed to Array.map() gets called with each element in the array and its index.
On each iteration, we modify the value of the current element using the index and return the result.
The map()
method returns a new array containing the values returned from the
callback function.
Array.reduce()
This is a two-step process:
Array.reduce()
method to iterate over the array.const arr = ['bobby', 'hadz', 'com']; const newArray = arr.reduce((accumulator, element, index) => { return [...accumulator, element + index]; }, []); // ๐๏ธ [ 'bobby0', 'hadz1', 'com2' ] console.log(newArray);
The function we passed to the Array.reduce() method gets called for each element in the array.
We initialized the accumulator
variable to an empty array because that's what
we passed as the second argument to the reduce()
method.
The value we return from the callback function gets passed as the accumulator
on the next iteration.
On each iteration, we use the spread syntax (...) to unpack the values of the array into a new array and add the current element + its index to the end of the array.
The Array.reduce()
method returns a new array and doesn't mutate the original.
for
loopThis is a two-step process:
for
loop to iterate over the array.const arr = ['a', 'b', 'c']; for (let index = 0; index < arr.length; index++) { arr[index] = arr[index] + index; } console.log(arr); // ๐๏ธ ['a0', 'b1', 'c2']
The code sample is similar to the previous one that used the Array.forEach()
method.
However, we used a for
loop to iterate over the array.
It's a matter of personal preference but in my opinion the forEach()
approach
is more readable and intuitive.
Most experienced developers avoid mutating arrays and objects. Instead, they create new arrays containing the desired elements.
We can use the Array.map()
or Array.reduce()
methods to achieve this.
Array.fill()
You can use the fill()
method to set the elements on an array to a specific
value.
The fill
method changes the elements in an array, setting them to a static
value and returns the modified array.
const arr = new Array(3).fill('value'); // ๐๏ธ ['value', 'value', 'value'] console.log(arr);
We called the Array.fill() method on an existing array with a set length to fill the array with a specific value.
You can also use an existing array's length to determine the length of the new array.
const existingArr = [1, 2, 3]; const newArr = new Array(existingArr.length).fill(100); // ๐๏ธ [100, 100, 100] console.log(newArr);
The argument we passed to the fill()
method is the value with which to fill
the array.
The
Array()
constructor creates an array of N empty elements, which we can fill using the
fill()
method.
// ๐๏ธ [,,] console.log(new Array(3))
You can learn more about the related topics by checking out the following tutorials: