Skip to content

Instantly share code, notes, and snippets.

@stpe
Created April 6, 2014 20:23
Show Gist options
  • Save stpe/10011081 to your computer and use it in GitHub Desktop.
Save stpe/10011081 to your computer and use it in GitHub Desktop.
Using Array.prototype.reduce; Given an array of values, returns array of the sums of the values per INTERVAL.
var INTERVAL = 5;
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
list.reduce(function(result, value, index) {
var i = Math.floor(index/INTERVAL);
result[i] ? result[i] += value : result[i] = value;
return result;
}, []);
// Output: [15, 40, 11]
@pixfy
Copy link

pixfy commented Jul 15, 2022

It was at what i was looking for and worked perfect!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment