14

I am trying to increment the numbers in the array

var myArray = [1, 2, 3, 4];

I try to use

for (var i = 0; i < myArray.length; i++){
 myArray[i] + 1;
}

but that doesn't seem to do anything :( please help

Zakaria Acharki
67.5k15 gold badges79 silver badges106 bronze badges
asked Sep 26, 2015 at 10:36
1
  • easiest and simplest way is myArray.map(i => ++i); Commented Aug 24, 2022 at 10:36

10 Answers 10

13

Use the ES6 arrow function:

arr = [1, 2, 3, 4];
new_arr = arr.map(a => a+1);
console.log(new_arr);

answered Jul 17, 2017 at 18:11
Sign up to request clarification or add additional context in comments.

Comments

13

You can use map() which will make it quite clean:

var arr = [1,2,3,4];
arr = arr.map(function(val){return ++val;});
console.log(arr);

answered Sep 26, 2015 at 10:41

Comments

10

There's many possibilities to do that, you can use plus equal += like following :

for (var i = 0; i < myArray.length; i++){
 myArray[i] += 1;
}

Or simply :

for (var i = 0; i < myArray.length; i++){
 myArray[i] = myArray[i] + 1;
}

Hope this helps.


var myArray = [1, 2, 3, 4];
for (var i = 0; i < myArray.length; i++){
 myArray[i] += 1;
}
alert(myArray);

answered Sep 26, 2015 at 10:38

Comments

2

Assuming your array contains ordered numbers, with increment of size 1, you can also use this code:

var myArray = [1,2,3,4];
myArray.push(myArray[myArray.length - 1] + 1);
myArray.shift();
alert(myArray);

Zakaria Acharki
67.5k15 gold badges79 silver badges106 bronze badges
answered Sep 26, 2015 at 10:46

Comments

1

Without Es6,

 myArray[i] = myArray[i] + 1;
 or
 ++myArray[i]

Will work.

answered Nov 4, 2019 at 11:20

1 Comment

This is not returning the next element in the array, it is increasing the adding 1 to the current element
1

You can use Array constructor.

Using Array.map() method, fill the series with numbers

For Example :

Array(5 + 1).fill().map((_, index) => index + 1)
answered Oct 1, 2021 at 11:17

Comments

0

You can use ES6's Array.from() method. The callback is a mapping function, which you can use to add one to each number in the array.

Here the prefix (rather than postfix) increment operator ++ is used in the map function because when you use the increment operator before the operand, the increment occurs before the value is returned, whereas with postfix, the original value will be returned before the operand will be increased (so, an unchanged version of the array would be returned in the case of x++).

function addOne(arr) {
 return Array.from(arr, x => ++x);
}
 
addOne([1, 2, 3]); // [2, 3, 4]
answered Aug 31, 2021 at 5:10

Comments

0

const arr1 = Array.from(Array(5), (_, index) => index);
console.log(arr1); // 👉️ [ 0, 1, 2, 3, 4 ]
const arr2 = Array.from(Array(5), (_, index) => index + 1);
console.log(arr2); // 👉️ [ 1, 2, 3, 4, 5 ]

answered May 6, 2023 at 4:26

1 Comment

This creates a new array containing a sequence, it does not increment the values of an existing array, right?
0

An ES6 functional solution would be:

myArray.forEach((_, i) => myArray[i]++);
// or
myArray.forEach((_, i, a) => a[i]++);

This is basically the same as Zakaria Acharki's answer. The arguments passed to the arrow function by .forEach() are the element themselves, the index and a reference to the array. Since the values are, in this particular situation, numbers, the _ variable holds a copy of the value, and incrementing _ won't affect anything outside our function's scope. However, myArray[i] or a[i] is a reference, which works as expected when we increment it.

Try it:

console.config({ maximize: true });
const myArray = [1, 2, 3, 4];
myArray.forEach((_, i) => myArray[i]++);
console.log(myArray);
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

When this proposal is accepted (currently it is at stage 3) and supported by browsers, you can do something like this:

myArray.keys().forEach(i => myArray[i]++);

Array#keys() returns an iterator that yields the array's keys, which are its numeric indices. .forEach() works the same as Array#forEach(); the difference between this example and the example above (not the methods themselves) is that the first argument is now also the index, so we wouldn't need the second one.

Try it (as of May 2023, this doesn't work):

console.config({ maximize: true });
const myArray = [1, 2, 3, 4];
myArray.keys().forEach(i => myArray[i]++);
console.log(myArray);
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

answered May 9, 2023 at 20:23

Comments

0

You can now do this with Array.reduce() (MDN doc):

array = [23,10]
array.reduce((accumulator, currentValue) => accumulator + currentValue)
// Returns 33
answered Oct 24, 2023 at 17:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.