
How to Find Shopify Product ID Number
October 23, 2018
How to create full width (100% ) container inside fixed width container
May 9, 2019I need this function pretty often that’s why I just leave it here.
Suppose we have an array:
var array = [1, 2, 3, 4, 5];
In the result we need something like that:
var array2 = [0, 1, 2, 3, 4];
How to subtract 1 from each number in the array
There are two simple ways to do that.
Using map method
You can use .map ().
The solution will be:
var array2 = array.map( function(value) {
return value - 1;
} );
Using for-loop method
Code for the second one is longer but it is here for the demonstration.
// Create an array to hold our new values
var array2 = [];
// Iterate through each element in the original array
for(var i = 0; i < array.length; i++) {
// Decrement the value of the original array and push it to the new one
array2.push(array[i] - 1);
}
"- 1" can be changed to any other math operation like "+ 2", "*6", "/1.2", etc.


