Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the total value of the matching array elements

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
like image 241
themanwiththemasterplan Avatar asked Sep 18 '18 08:09

themanwiththemasterplan


People also ask

How do you count matching items in an array?

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.

How do you find the matching element in two arrays?

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.


2 Answers

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);
like image 143
Ori Drori Avatar answered Sep 19 '22 06:09

Ori Drori


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);
like image 24
Leonid Pyrlia Avatar answered Sep 21 '22 06:09

Leonid Pyrlia