How to Add a Number to Every Item in an Array?
Last Updated :
19 Jul, 2024
Adding a number to every item in an array is a common operation in JavaScript, often required for tasks like incrementing values or performing mathematical operations on array elements.
Below are the approaches to add a number to every item in an array:
Using Array.map() Method
The map() method creates a new array by applying a provided function to each element of the original array.
Syntax:
const newArray = originalArray.map(item => item + numberToAdd);
Example: To demonstrate adding a number to every element in an array using the JavaScript built-in method array.map() method.
JavaScript
const GFG = [1, 2, 3, 4, 5];
const numberToAdd = 10;
const newArray = GFG
.map(item => item + numberToAdd);
console.log(newArray);
Output[ 11, 12, 13, 14, 15 ]
Using a for Loop
We can iterate through the array using a for loop and add the number to the each element individually. the for loop iterates through each element and updates it.
Syntax:
for (let i = 0; i < originalArray.length; i++)
{
originalArray[i] += numberToAdd;
}
Example: To demonstrate adding a number to every item in an array using the for loop in JavaScript.
JavaScript
const originalArrays = [1, 2, 3, 4, 5];
const numberToAdd = 10;
for (let i = 0; i < originalArrays.length; i++) {
originalArrays[i] += numberToAdd;
}
console.log(originalArrays);
Output[ 11, 12, 13, 14, 15 ]
Using reduce Method
The reduce() method executes a reducer function on each element of the array.
Syntax:
newArray = array.reduce((accumulator, currentValue) => accumulator.push(currentValue + numberToAdd)
Example: To demonstrate adding a number to every item in an array using the reduce method in JavaScript.
JavaScript
let array = [1, 2, 3, 4];
let numberToAdd = 5;
let newArray = array.reduce((accumulator, currentValue) => {
accumulator.push(currentValue + numberToAdd);
return accumulator;
}, []);
console.log(newArray);
Using forEach() Method
The forEach() method executes a provided function once for each array element. This approach is useful for performing operations on each element of the array without returning a new array.
Syntax
originalArray.forEach((item, index, arr) => {
arr[index] = item + numberToAdd;
});
Example: To demonstrate adding a number to every element in an array using the JavaScript built-in forEach() method.
JavaScript
// Original array
const originalArray = [1, 2, 3, 4, 5];
const numberToAdd = 2;
// Using forEach to add number to each element
originalArray.forEach((item, index, arr) => {
arr[index] = item + numberToAdd;
});
// Output the modified array
console.log('Modified array:', originalArray);
OutputModified array: [ 3, 4, 5, 6, 7 ]
Using the Array.from() Method
The Array.from() method creates a new array instance from an array-like or iterable object. By providing a mapping function as the second argument, we can add a number to every item in the array.
Example: The below example demonstrates how to add a number to every item in an array using the Array.from() method in JavaScript.
JavaScript
// Original array
const originalArray = [1, 2, 3, 4, 5];
// Number to add
const numberToAdd = 3;
// Using Array.from() method to add a number to every item in the array
const newArray = Array.from(originalArray, item => item + numberToAdd);
console.log(newArray);
Similar Reads
How to Add the Numbers in a JavaScript Array? Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo
2 min read
How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati
4 min read
How to Find the Sum of an Array of Numbers in JavaScript ? To find the sum of an array of numbers in JavaScript, you can use several different methods.1. Using Array reduce() MethodThe array reduce() method can be used to find the sum of array. It executes the provided code for all the elements and returns a single output.Syntaxarr.reduce( callback( acc, el
2 min read
How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app
5 min read
How to add elements to an existing array dynamically in JavaScript ? An array is a JavaScript object that can hold multiple values at a time, irrespective of the data type. In this article, we will see how to add new elements to an existing array dynamically in Javascript. We will discuss two methods in this article i.e. push() method and dynamically adding an elemen
4 min read
How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one
2 min read