Created
April 6, 2014 20:23
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was at what i was looking for and worked perfect!!!