1console.log("hello-world);
javascript
console.log("hello-world);
hidden codeblock for CSS
css
.container {
  width: 80%;
}
hidden codeblock for CSS
html
<pre><code class="language-css">
.container {
  width: 80%;
}
</code></pre>
hidden codeblock for CSS
1console.log("hello-world);
javascript
console.log("hello-world);
hidden codeblock for CSS
css
.container {
  width: 80%;
}
hidden codeblock for CSS
html
<pre><code class="language-css">
.container {
  width: 80%;
}
</code></pre>
hidden codeblock for CSS

How to modify all the Array elements in JavaScript

ReadTime: 4 minutes

Updating or modifying all the array elements is a frequent need in Javascript (or even React JS)? in this blogpost, lets see how can we do it.


Using _**Array.map()**_

The Array.map() can create a new array after applying a (mapping) function to each element in the original array. Let’s see how it helps us update all elements.

javascript
const originalArray = [1, 2, 3, 4, 5];
  
const updatedArray = originalArray.map(element => element * 2);

console.log(updatedArray); // πŸ‘‰οΈ [2, 4, 6, 8, 10]
console.log(originalArray); // πŸ‘‰οΈ [1, 2, 3, 4, 5] (original array remains unchanged)
Using Array.map() to update array elements

In this example, we’ve doubled each element in the array without modifying the original array.

Using **_Array.reduce()_**

Array.reduce() is your go-to method for transforming an array into a single value or a more complex structure. While its primary purpose is not updating elements directly, we can leverage it for the task.

javascript
const originalArray = [1, 2, 3, 4, 5];
  
const updatedArray = originalArray.reduce((acc, element) => {
  acc.push(element * 2);
  return acc;
}, []);

console.log(updatedArray); // πŸ‘‰οΈ [2, 4, 6, 8, 10]
console.log(originalArray); // πŸ‘‰οΈ [1, 2, 3, 4, 5] (original array remains unchanged)
Using Array.reduce() to accumulate and collect transformed array elements

In this example, we’ve used Array.reduce() to build a new array by doubling each element.

Using for Loop: Classic approach

Sometimes, the old ways are the best ways. A simple for loop is a classic approach to updating array elements.

javascript
const originalArray = [1, 2, 3, 4, 5];
const updatedArray = [];

for (let i = 0; i < originalArray.length; i++) {
  updatedArray.push(originalArray[i] * 2);
}

console.log(updatedArray); // πŸ‘‰οΈ [2, 4, 6, 8, 10]
console.log(originalArray); // πŸ‘‰οΈ [1, 2, 3, 4, 5] (original array remains unchanged)
Using for loop to update array elements iteratively

This loop iterates through the original array, doubling each element and pushing it to a new array.

Using Array.fill(): Set Them All

When you want to set all elements in an array to a specific value, you can use Array.fill().

javascript
const originalArray = [1, 2, 3, 4, 5];
const updatedArray = new Array(originalArray.length).fill(42);

console.log(updatedArray); // πŸ‘‰οΈ [42, 42, 42, 42, 42]
console.log(originalArray); // πŸ‘‰οΈ [1, 2, 3, 4, 5] (original array remains unchanged)
Array.fill() to set all set all elements of an array in (JavaScript)

Here, we’ve created a new array filled with the value 42, effectively updating all elements.


πŸ§ͺPractice Coding Problem: Square Array Elements ⏹

In the spirit of Test Driven Development ( 😁), lets test our understanding by solving a problem.

Implement a function that upgrades each element in an array by squaring it

Problem (JavaScript)

javascript
/**
* Upgrade all elements in the array by squaring them.
* @param {number[]} array - The original array.
* @returns {number[]} - The upgraded array.
*/
function upgradeArray(array) {
  // > > > πŸ‘‰ Write code here πŸ‘ˆ < < <
}

const originalArray = [2, 3, 5, 7];
console.log(upgradeArray(originalArray)); // πŸ‘‰οΈ [4, 9, 25, 49]
Problem Code

Please attempt before seeing the Answer:

Solution
javascript
function upgradeArray(array) {
  return array.map(element => element ** 2);
}
Solution Code

Now you know ways to keep your Javascipt arrays updated, always. ✨

Keep updating and keep coding πŸš€!

Contact

Feel free to reach out!