Skip to content

Instantly share code, notes, and snippets.

@tarzak
Last active August 29, 2015 14:27
Show Gist options
  • Save tarzak/71af151f1acecb254e9e to your computer and use it in GitHub Desktop.
Save tarzak/71af151f1acecb254e9e to your computer and use it in GitHub Desktop.
var arr = [
{ccy:"EUR", balance:1000},
{ccy:"USD", balance:"2700"},
{ccy:"EUR", balance:"5600"},
{ccy:"USD", balance:"2500"}
]
var newArr = [];
arr.reduce(function(previousValue, currentValue, index, array) {
if (index === 0) newArr.push({y: +currentValue.balance, name:currentValue.ccy})
if (index > 0) {
var mark = 0;
for (var i = 0; i < newArr.length; i += 1) {
if (newArr[i] && newArr[i].name === currentValue.ccy) {
newArr[i].y += +currentValue.balance;
mark = 1;
}
}
if (!mark) newArr.push({y:+currentValue.balance, name:currentValue.ccy});
}
}, newArr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment