This is my current array
0:{modelNumber: "123456789", balance: { amount:1000, currency:"EUR" }}
1:{modelNumber: "987654321", balance: { amount:2000, currency:"EUR" }}
2:{modelNumber: "322353466", balance: { amount:1500, currency:"GBP" }}
3:{modelNumber: "892347522", balance: { amount:1000, currency:"USD" }}
4:{modelNumber: "931883113", balance: { amount:3000, currency:"INR" }}
5:{modelNumber: "854300564", balance: { amount:2500, currency:"GBP" }}
6:{modelNumber: "931883113", balance: { amount:3000, currency:"INR" }}
7:{modelNumber: "854300564", balance: { amount:3500, currency:"USD" }}
I'm trying to return a new array, with each currency and the total value for each currency.
Like below return the total amount for each currency in the array above
0:{currency: "EUR", totalAmount: 3500}
1:{currency: "GBP", totalAmount: 5000}
2:{currency: "USD", totalAmount: 4500}
3:{currency: "INR", totalAmount: 6000}
My approach initially:
//the current array
let theInitialArray = state.vehicle;
const results = theInitialArray.reduce((accumalator, current) => {
const { currency } = current.balance;
if (accumalator[currency]) {
accumalator[currency].push(current);
return accumalator;
}
accumalator[currency] = [current];
return accumalator;
}, {});
let frank = Object.keys(results)
let jim = [];
let expectedOutput = theInitialArray.filter((x) => {
for (let i=0; i < frank.length; i++) {
if (x.balance.currency === frank[i]) {
jim.push({'currency': frank[i], 'amount': x.balance.amount});
}
}
});
console.log('expectedOutput', expectedOutput)
return expectedOutput
If you want to count how many items in an array (or any collection) match a test you specify, the easiest thing to do is run the collection through a call to filter() then count the remainder.
Approach: Get the two java Arrays. Iterate through each and every element of the arrays one by one and check whether they are common in both. Add each common element in the set for unique entries.
You can Array.reduce()
to iterate the data. If a currency doesn't exist in the accumulator (r
in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values
:
const data = [{"modelNumber":"123456789","balance":{"amount":1000,"currency":"EUR"}},{"modelNumber":"987654321","balance":{"amount":2000,"currency":"EUR"}},{"modelNumber":"322353466","balance":{"amount":1500,"currency":"GBP"}},{"modelNumber":"892347522","balance":{"amount":1000,"currency":"USD"}},{"modelNumber":"931883113","balance":{"amount":3000,"currency":"INR"}},{"modelNumber":"854300564","balance":{"amount":2500,"currency":"GBP"}},{"modelNumber":"931883113","balance":{"amount":3000,"currency":"INR"}},{"modelNumber":"854300564","balance":{"amount":3500,"currency":"USD"}}];
const result = Object.values(data.reduce((r, { balance }) => {
const { amount, currency } = balance;
if(!r[currency]) r[currency] = { currency, amount: 0 };
r[currency].amount += amount;
return r;
}, {}));
console.log(result);
A simple solution with Array.prototype.reduce
and Object.keys
:
const data = [
{modelNumber: "123456789", balance: { amount:1000, currency:"EUR" }},
{modelNumber: "987654321", balance: { amount:2000, currency:"EUR" }},
{modelNumber: "322353466", balance: { amount:1500, currency:"GBP" }},
{modelNumber: "892347522", balance: { amount:1000, currency:"USD" }},
{modelNumber: "931883113", balance: { amount:3000, currency:"INR" }},
{modelNumber: "854300564", balance: { amount:2500, currency:"GBP" }},
{modelNumber: "931883113", balance: { amount:3000, currency:"INR" }},
{modelNumber: "854300564", balance: { amount:3500, currency:"USD" }}
];
const grouped = data.reduce((o, { balance: { amount:a, currency:c } }) =>
({...o, [c]: (o[c] || 0) + a }), {});
const result = Object.keys(grouped).map(currency =>
({currency, totalAmount: grouped[currency] }));
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