How to subtract 1 from each number in an array?

If i have

var numberarr = [1, 2, 3, 4, 5]; 

How can i do this in

 var numberarr2 = [0, 1, 2, 3, 4]; 

decreasing 1 of each element?

+5
source share
2 answers

You can use .map( )

 var numberarr2 = numberarr.map( function(value) { return value - 1; } ); 
+21
source

Try the following:

 // Create an array to hold our new values var numberArr2 = []; // Iterate through each element in the original array for(var i = 0; i < numberArr1.length; i++) { // Decrement the value of the original array and push it to the new one numberArr2.push(numberArr1[i] - 1); } 
+2
source

Source: https://habr.com/ru/post/1233450/


All Articles