I have this array of numbers in JS code:
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
I need to sum the each number with the numbers before.
var cumulative12ArrOfNumbers = [10, 30, 60, 100, 150, 210, etc to 11 index... ];
What is the best way to do that in JS?
UPD : I'm newbie in JS
I have this array of numbers in JS code:
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
I need to sum the each number with the numbers before.
var cumulative12ArrOfNumbers = [10, 30, 60, 100, 150, 210, etc to 11 index... ];
What is the best way to do that in JS?
UPD : I'm newbie in JS
Share Improve this question edited Jan 6, 2017 at 9:22 Max Gabderakhmanov asked Jan 6, 2017 at 9:14 Max GabderakhmanovMax Gabderakhmanov 9221 gold badge18 silver badges36 bronze badges 4- What the code do you want? I want the solution from you. I've got just these arrays – Max Gabderakhmanov Commented Jan 6, 2017 at 9:19
- Nope that's not how SO works. You need to e up with a specific problem, and an attempt at a solution. Then we troubleshoot your code. You just want us to write code for you. That won't fly. – dda Commented Jan 6, 2017 at 9:36
- 1 In case you wonder, the downvotes are because you don't show research efforts – Randy Commented Jan 6, 2017 at 9:40
- @dda thanks for the explanation – Max Gabderakhmanov Commented Jan 6, 2017 at 9:44
6 Answers
Reset to default 11You could use Array#map
and return the sum of the item and the last sum.
This proposal does not alter the original array.
var array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
result = array.map(function (a) {
return this.last += a;
}, { last: 0 });
console.log(result);
A version without thisArg
and a closure
var array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
result = array.map(function (last) {
return function (a) {
return last += a;
};
}(0));
console.log(result);
ES6
var array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
result = array.map((last => a => last += a)(0));
console.log(result);
Simple and fast solution using a regular for
loop(without any functions):
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
for (var i = 0, len = arrOf12Numbers.length; i < len; i++) {
if (i > 0) arrOf12Numbers[i] = arrOf12Numbers[i] + arrOf12Numbers[i - 1];
}
console.log(arrOf12Numbers);
And it's more than 2 times faster than approach with map()
function(if you care about performance)
To get result as a new array use the following approach:
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
result = arrOf12Numbers.slice();
for (var i = 0, len = result.length; i < len; i++) {
if (i > 0) result[i] = result[i] + result[i - 1];
}
console.log(result);
The second apprroach is still about 2 times faster than map()
function approach
This will do the work without altering the original array. You just have to pass your original array to the function.
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
var myFunc = function(arrOf12Numbers) {
var cumulative12ArrOfNumbers = [];
cumulative12ArrOfNumbers[0] = arrOf12Numbers[0];
for(i=1; i<arrOf12Numbers.length; i++) {
cumulative12ArrOfNumbers[i] = arrOf12Numbers[i-1] + arrOf12Numbers[i];
}
console.log(cumulative12ArrOfNumbers);
};
myFunc(arrOf12Numbers);
This might help you.
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
var result = [];
var sum = 0;
arrOf12Numbers.forEach(function (val, key) {
sum += val;
result.push(sum);
});
console.log(result);
var arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
for (var i = 1; i < arr.length; i++) {
sum = arr[i] + arr[i-1];
arr[i] = sum;
}
It's not really so plicated, just a loop;
var arrOf12Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
var output = [], sum;
for (var i = 0; i < arrOf12Numbers.length;i++)
{
output.push(i === 0 ? arrOf12Numbers[i] : output[i-1] + arrOf12Numbers[i])
}
console.log(output);