I have 2 arrays that looks like this:
amountArray = ["200","150","500","100"];
currencyArray = ["EUR","EUR","USD","USD"];
I'd like to sum amounts from 1st array if 2nd array match. Result i'd like to get :
totalAmount = "350 EUR | 600 USD";
You could take a Map for collecting same currencies and get the joined values.
let amounts = ["200", "150", "500", "100"],
currencies = ["EUR", "EUR", "USD", "USD"],
result = Array
.from(
currencies.reduce(
(m, c, i) => m.set(c, (m.get(c) || 0) + +amounts[i]),
new Map
),
([k, v]) => [v, k].join(' ')
)
.join(' | ');
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With