Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created June 13, 2016 12:14
Show Gist options
  • Save peat-psuwit/2e661d23e36f0564028f6958aac98bb3 to your computer and use it in GitHub Desktop.
Save peat-psuwit/2e661d23e36f0564028f6958aac98bb3 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
function sumSeq(data) {
var groups = _(data).groupBy('group').map(function(value) {
return {
members: _.orderBy(value, ['seq'], ['desc']),
sumSeq: _.sumBy(value, 'seq')
};
}).orderBy(['sumSeq'], ['desc']).value();
_.forEach(groups, function(value) {
console.log(value.members[0].group + " " + value.sumSeq + ": " +
value.members[0].name + "(" + value.members[0].seq + ")");
});
}
sumSeq([
{"name": "Man", "group": "A", "seq": 8},
{"name": "John", "group": "A", "seq": 1},
{"name": "Dex", "group": "B", "seq": 2},
{"name": "Bomb", "group": "A", "seq": 5},
{"name": "Joii", "group": "C", "seq": 7},
{"name": "New", "group": "B", "seq": 3},
{"name": "Keng", "group": "B", "seq": 4},
{"name": "Tan", "group": "C", "seq": 6}
]);
/*
* Should print:
* A 14: Man(8)
* C 13: Joii(7)
* B 9: Keng(4)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment