Skip to content

Instantly share code, notes, and snippets.

@asterion
Created May 1, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asterion/34064cbefb7e55336b42c27f20fdab20 to your computer and use it in GitHub Desktop.
Save asterion/34064cbefb7e55336b42c27f20fdab20 to your computer and use it in GitHub Desktop.
json data
var data = [
{type: "a", income: 1},
{type: "a", income: 1},
{type: "e", income: 1},
{type: "c", income: 1},
{type: "d", income: 1},
{type: "d", income: 1},
{type: "d", income: 1},
];
var total = {};
for(i=0; i < data.length; i++){
if (total[data[i].type] == undefined) {
total[data[i].type] = 0;
}
total[data[i].type] = total[data[i].type] + data[i].income;
}
console.log(total.a);
console.log(total.c);
console.log(total.d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment