Having trouble finding the most efficient solution to this problem. I attempted using a loop, but I can't seem to make any progress. Any assistance would be greatly appreciated.
This is my current data:
[ [ ‘TWENTY’, 2000 ],
[ ‘TWENTY’, 2000 ],
[ ‘TWENTY’, 2000 ],
[ ‘TEN’, 1000 ],
[ ‘TEN’, 1000 ],
[ ‘FIVE’, 500 ],
[ ‘FIVE’, 500 ],
[ ‘FIVE’, 500 ],
[ ‘ONE’, 100 ],
[ ‘QUARTER’, 25 ],
[ ‘QUARTER’, 25 ],
[ ‘DIME’, 10 ],
[ ‘DIME’, 10 ],
[ ‘PENNY’, 1 ],
[ ‘PENNY’, 1 ],
[ ‘PENNY’, 1 ],
[ ‘PENNY’, 1 ] ]
I am aiming for:
[ [ ‘TWENTY’, 6000 ],
[ ‘TEN’, 2000 ],
[ ‘FIVE’, 1500 ],
[ ‘ONE’, 100 ],
[ ‘QUARTER’, 50 ],
[ ‘DIME’, 20 ],
[ ‘PENNY’, 4 ] ]
My attempt at solving it so far:
newArr = [ [ 'TWENTY', 0 ],
[ 'TEN', 0 ],
[ 'FIVE', 0 ],
[ 'ONE', 0 ],
[ 'QUARTER', 0 ],
[ 'DIME', 0 ],
[ 'PENNY', 0 ] ];
for (var z = 0; z < arr.length; z++) {
for (var y = 0; y < newArr.length; y++) {
if (newArr[y][0] == arr[z][0]) {
newArr[y][1] = newArr[y][1] + arr[z][1]
}
}
}