Commit 387f249b authored by l2m2's avatar l2m2

upload

parent d78eb1b8
var _ = require("lodash");
var arr = [
{ name: 'A', a: 1, b: 2 },
{ name: 'A', a: 2, b: 3 },
{ name: 'B', a: 1, b: 2 },
{ name: 'C', a: 4, b: 5 },
{ name: 'C', a: 5, b: 6 },
{ name: 'C', a: 1, b: 1 },
{ name: 'D', a: 7, b: 8 }
];
var r = _.chain(arr).reduce(function(prev, curr){
if (_.has(prev, curr.name)) {
var temp = prev[curr.name];
temp.a = temp.a + curr.a;
temp.b = temp.b + curr.b;
} else {
prev[curr.name] = curr;
}
return prev;
}, {}).value();
print(JSON.stringify(r, null, ' '));
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment