8

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!

4
  • You are mixing up data here which might be a problem to verify right values. E.g., something like $(item).outerHeight() should not be part of your function. Commented Nov 3, 2017 at 12:02
  • var_nn=[];var sum = fbDivHeights.reduce(function(a, b) {return _nn.push( a + b); }); Commented Nov 3, 2017 at 12:02
  • 1
    @GeorgeJempty, that is not the duplicate of that you wrote. Commented Nov 3, 2017 at 12:04
  • 1
    @Alexandru-IonutMihai I know, I rescinded my close vote Commented Nov 3, 2017 at 16:31

6 Answers 6

14

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);

Sign up to request clarification or add additional context in comments.

Comments

11

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);

4 Comments

I just don't understand
@Faly, this code is equivalent to array.map(function (s) { return function (a) { return s += a; }; }(0));
Does a => s += a returns s + a ?
@Faly, right it's an addition assignment += operator.
3

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);

Comments

2

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

Comments

1

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);

Comments

1

Yet another reducer ;)

const cumulative = [280,430,408,430,408]
       .reduce( (p, n) => p.concat((+p.slice(-1) || 0) + n), []);
 
console.log(cumulative);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.