Skip to content

Instantly share code, notes, and snippets.

@wallyuva
Last active January 10, 2017 20:14
Show Gist options
  • Select an option

  • Save wallyuva/0059c66fbef10f438262c505acdf4389 to your computer and use it in GitHub Desktop.

Select an option

Save wallyuva/0059c66fbef10f438262c505acdf4389 to your computer and use it in GitHub Desktop.
Reduce sample
var data = [
{
column1: 1.0,
column2: 2.0
},
{
column1: 1.1,
column2: 2.0
},
{
column1: 1.1,
column2: 2.1
},
];
var grouped = data.reduce( function (prev, item) {
if (!prev[item.column1]) {
prev[item.column1] = {
group: [item.column2]
}
} else {
prev[item.column1].group.push(item.column2);
}
return prev;
}, {} );
console.dir(grouped, { depth: null });
var array = Object.keys(grouped).map(function (key) {
return {
column1: key,
group: grouped.group
}
});
console.dir(array, { depth: null});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment