I have a javascript array with these values:
fbDivHeightsTest = [280,430,408,430,408]
I need a new array with the sum of elements like this:
newArray = [280, 710, 1118, 1548, 1956].
The fbDivHeightsTest[0] + fbDivHeightsTest[1], fbDivHeightsTest[0] + fbDivHeightsTest[1] + fbDivHeightsTest[2]
and so on.
I have tried like this, but the reduce function is not returning the result I expect. It returns: 280, 710, 1398, 2818, 5614
sectionsSeclector.each(function(index, item) {
var sum = fbDivHeights.reduce(function(a, b) { return a + b; }, 0);
fbDivHeights.push( $(item).outerHeight() + sum);
});
Can you please help me with this? thanks!
You could use a closure over the actual sum and map the incremented result.
How it works :
Assuming the function is called, then
(s => a => s += a)(0) // ^ value for s
it returns
a => s += a // s = 0
for the callback of
Array#map
and for every loop,280 => s += 280 // s = 280 430 => s += 430 // s = 710 408 => s += 408 // s = 1118 430 => s += 430 // s = 1548 408 => s += 408 // s = 1956
var array =[280, 430, 408, 430, 408], result = array.map((s => a => s += a)(0)); console.log(result);
You can use reduce
method in combination with map
method.
The reduce()
method applies a function against an accumulator and each element in the array
(from left to right) to reduce it to a single value.
let array = [280,430,408,430,408]; array = array.map((elem, index) => array.slice(0,index + 1).reduce((a, b) => a + b)); console.log(array);
You can also use only map
method.
let array = [280,430,408,430,408],sum; array = array.map(elem => sum = (sum || 0) + elem); console.log(array);
javascript reduce() function will do the job for you easily:-
var fbDivHeightsTest = [280,430,408,430,408] var new_array = []; fbDivHeightsTest.reduce(function(a,b,i) { return new_array[i] = a+b; },0); console.log(new_array);
Just use array.reduce:
var fbDivHeightsTest = [280,430,408,430,408] ; var res = fbDivHeightsTest.reduce((m, o, i) => { var prev = m[i - 1]; if (prev) { o = o + prev; } m.push(o); return m; }, []); console.log(res);
the reduce()
(sometimes also called fold) method is intended to "reduce" an Array to a single value (although that single value can be an object or list)
In your example, you son't want a sum of the list, but you want to map (1:1) for each value in the initial arrray, the sum of the values to this point:
let array = [280,430,408,430,408]; let _sum = 0; let sums = array.map(value => _sum += value); console.log(sums);
Or as mentioned, you reduce one list to another:
let array = [280, 430, 408, 430, 408]; let sums = array.reduce((results, value, index) => { let previousSum = index>0? results[index-1]: 0; results.push(previousSum + value); return results; }, []); console.log(sums);
where results
is an intermediate Result
Yet another reducer ;)
const cumulative = [280,430,408,430,408] .reduce( (p, n) => p.concat((+p.slice(-1) || 0) + n), []); console.log(cumulative);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.