Created
December 16, 2013 05:18
-
-
Save tjstebbing/7982647 to your computer and use it in GitHub Desktop.
This file contains 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
function groupData(data, nGroups) { | |
var out = []; | |
data.sort(function(a, b) { return a > b; }) | |
var hop = data[data.length-1] / nGroups; | |
for(var i=0; i<data.length; i++) { | |
var index = Math.ceil(data[i] / hop); | |
if(!out[index]) out[index] = []; | |
out[index].push(data[i]); | |
} | |
return out; | |
} | |
console.log(groupData([1,2,3,4,5,100, 40,600,6000], 10)); | |
console.log(groupData([1,2,3,4,5], 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment