Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With JavaScript how would I increment numbers in an array using a for loop?

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

like image 609
lateralus500 Avatar asked Sep 26 '15 10:09

lateralus500


1 Answers

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);
like image 175
baao Avatar answered Sep 26 '22 06:09

baao