-2

What would be the easiest way to solve this? I've tried a loop, but I can't really get anything to work. Really need some help out here.

This is what I have :

[ [ ‘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 want:

[ [ ‘TWENTY’, 6000 ],
    [ ‘TEN’, 2000 ],
    [ ‘FIVE’, 1500 ],
    [ ‘ONE’, 100 ],
    [ ‘QUARTER’, 50 ],
    [ ‘DIME’, 20 ],
    [ ‘PENNY’, 4 ] ]

solved :

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]
    }
  }

}
Eddie
  • 1
  • 1
  • 3
    Please show the code you tried. – lurker Jul 21 '20 at 20:06
  • Welcome to stack overflow! _"I've tried a loop, but I can't really get anything to work."_ Post the loop you tried, and we can tell you how to fix it. – Alex Wayne Jul 21 '20 at 20:06
  • simple loop, create object, add number together, loop over object and create your new array. – epascarello Jul 21 '20 at 20:06
  • 1
    Multiple ways this could be done, from simple (make a map w/ string keys and each time you see a key either create the first entry or add to the existing entry), or via `reduce` (same result but "fancy"), or... What have you tried so far? – Dave Newton Jul 21 '20 at 20:06

1 Answers1

-1

Here is what you want:

const arr = [['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]];

const b = arr.reduce((a,c) => {
    if (a.has(c[0])) {
        a.get(c[0])[1]+=c[1];
    } else {
        a.set(c[0],c);
    }
    return a;
},new Map());
console.log(b);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
8HoLoN
  • 1,122
  • 5
  • 14
  • 1
    This is not adding the numbers? – Eddie Jul 21 '20 at 20:25
  • In general it's better to teach a man to fish than to give him fish. (And it doesn't answer the question, and `filter` is best used for filtering, not non-filtering iteration.) – Dave Newton Jul 21 '20 at 20:28
  • Good point! I should be able to figure this out. – Eddie Jul 21 '20 at 20:33
  • I hadn't seen the need to add the numbers, answer updated! – 8HoLoN Jul 21 '20 at 20:52
  • I solved it a little different... – Eddie Jul 21 '20 at 20:56
  • newArr = [ [ 'TWENTY', 0 ], [ 'TEN', 0 ], [ 'FIVE', 0 ], [ 'ONE', 0 ], [ 'QUARTER', 0 ], [ 'DIME', 0 ], [ 'PENNY', 0 ] ]; for (var z = 0; z < returnChange.length; z++) { for (var y = 0; y < newArr.length; y++) { if (newArr[y][0] == returnChange[z][0]) { newArr[y][1] = newArr[y][1] + returnChange[z][1] } } } – Eddie Jul 21 '20 at 20:56