Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
Group By with Node / Javascript

I have a JS Array which also contains arrays with each holding a bunch of objects structured like below.

const arr1 = [
  [{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
  [{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]

What I need, is an output array, that groups all the child arrays by the "symbol" property and adds (+) the balances together.

At the End I want to have an array like this

[
{symbol: xy, summedBalance: 213131313}, 
{symbol: yz, summedBalance: 6788767867},
]

Is this possible with normal javascript or maybe a module like lodash?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try something like this :

const arr1 = [
  [{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
  [{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]

// Some variables
const knownSymbolsArr = [];
const finalArr = [];

// First degroup your array
const degroupedArr = arr1.flat();

// Iterate and sum balances
degroupedArr.forEach(el => {
  // Unknown symbol, process
  if (knownSymbolsArr.indexOf(el.symbol) === -1) {
    // Sum balances when symbol is el.symbol
    const balance = degroupedArr.reduce((acc, val) => val.symbol === el.symbol ? acc + val.balance : acc, 0);
    
    // Add symbol in our knownSymbolsArr
    knownSymbolsArr.push(el.symbol);
    
    // Add data in our final array
    finalArr.push({
      symbol: el.symbol,
      balance: balance
    });
  }
});

console.log(finalArr);

about 4 years ago · Juan Pablo Isaza Report

0

The solution can be broken down into two parts :

  1. Flatten the input array
  2. Group the elements of flattened array by symbol and calculate the sum.

const ar = [
  [{symbol: "xy", balance: 2155113}, {symbol: "asda", balance: 21231231}],
  [{symbol: "asda", balance: 6543}, {symbol: "xy", balance: 21678}]
]

const flattenedAr = ar.reduce((acc, curElement) => {
  acc = [...acc, ...curElement];
  return acc;
}, []);

const groupedAr = flattenedAr.reduce((group, curElement) => {

  const matchingElement = group.find(ele => ele.symbol === curElement.symbol);
  if(matchingElement) {
   matchingElement.summedBalance += curElement.balance;
  } else { 
   group.push({
     symbol: curElement.symbol,
     summedBalance: curElement.balance
   }); 
  }
   
  return group
}, [])

console.log(groupedAr)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!