Last updated: Mar 1, 2024
Reading timeยท5 min
To replace an element in an array:
Array.indexOf()
method to get the index of the element.const arr = ['a', 'b', 'c']; const index = arr.indexOf('a'); // ๐๏ธ 0 if (index !== -1) { arr[index] = 'z'; } console.log(arr); // ๐๏ธ ['z', 'b', 'c']
We used the Array.indexOf()
method to get the index of the array element with a value of a
.
const arr = ['a', 'b', 'c']; const index = arr.indexOf('a'); console.log(index); // ๐๏ธ 0
We then replaced the element at that index with a new value.
indexOf()
method returns -1
if it doesn't find an element with the supplied value.const arr = ['a', 'b', 'c']; const index = arr.indexOf('a'); // ๐๏ธ 0 if (index !== -1) { arr[index] = 'z'; } console.log(arr); // ๐๏ธ ['z', 'b', 'c']
We check if the method didn't return an index of -1
to be sure that an element
with the specified value exists.
0
and the last element has an index of arr.length - 1
.Alternatively, you can use the Array.splice()
method.
This is a three-step process:
indexOf()
method to get the index of the element to be replaced.Array.splice()
method to replace the element at the specific index.const arr = ['a', 'b', 'c']; const index = arr.indexOf('a'); // ๐๏ธ 0 arr.splice(index, 1, 'z'); console.log(arr); // ๐๏ธ [ 'z', 'b', 'c' ]
We passed the following 3 arguments to the Array.splice method:
We set the start
index to the index of the array element we want to replace.
const arr = ['a', 'b', 'c']; const index = arr.indexOf('a'); // ๐๏ธ 0 arr.splice(index, 1, 'z'); console.log(arr); // ๐๏ธ [ 'z', 'b', 'c' ]
delete count
argument to 1
, so the Array.splice()
method will remove the array element at the specified index and will add the provided third argument at the same index.In practice, we remove the array element at the specified index and then insert a different value at the same index, so we end up replacing the array element.
An alternative approach is to use a basic for
loop.
This is a three-step process:
array.length
iterations.for
loop.const arr = ['a', 'b', 'c']; for (let index = 0; index < arr.length; index++) { if (arr[index] === 'a') { arr[index] = 'z'; break; } } console.log(arr); // ๐๏ธ ['z', 'b', 'c']
We used a basic for
loop to iterate over the array. On each iteration, we
check if the current element is the one we want to replace.
Once we find and replace the element, we break
out of the loop to avoid
unnecessary work.
break
statement.const arr = ['a', 'b', 'c', 'a', 'a']; for (let index = 0; index < arr.length; index++) { if (arr[index] === 'a') { arr[index] = 'z'; } } console.log(arr); // ๐๏ธ [ 'z', 'b', 'c', 'z', 'z' ]
We didn't use a break
statement, so we replaced all elements with a value of
a
with elements with a value of z
.
An alternative solution is to not change the original array, but instead, create a new array containing the desired values.
We can use the Array.map()
method to achieve this.
This is a three-step process:
Array.map()
method to iterate over the array.const arr = ['a', 'b', 'c']; const newArr = arr.map(element => { if (element === 'a') { return 'z'; } return element; }); console.log(newArr); // ๐๏ธ ['z', 'b', 'c'] console.log(arr) // ๐๏ธ ['a', 'b', 'c']
The function we passed to the Array.map() method gets called with each element in the array.
map()
method returns a new array, containing the values we return from the callback function.In the example, we replace all array elements with a value of a
, setting them
to a value of z
.
We didn't change the contents of the original array and instead created a new array with the values we needed. This is often easier to reason about and track in larger code bases.
I've also written a detailed guide on how to filter an array of objects based on a property.
You can also use the Array.forEach()
method to replace the array elements in
place.
This is a three-step process:
Array.forEach()
method to iterate over the array.const arr = ['a', 'b', 'c', 'a', 'a']; arr.forEach((element, index) => { if (element === 'a') { arr[index] = 'z'; } }); // ๐๏ธ [ 'z', 'b', 'c', 'z', 'z' ] console.log(arr);
The function we passed to the Array.forEach()
method gets called with each
element in the array.
On each iteration, we check if the current element is the one to be replaced.
If the condition is met, we replace the element with the replacement value.
The forEach()
method is useful if you need to replace all occurrences of the
value in the array.
You can learn more about the related topics by checking out the following tutorials: