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

148
Views
Group and sum array of obects using Lodash

How to group an array of objects using object value, and then sum the column values of grouped objects. this is the sample object.

let salesData = [
   {
       'phone': 'iPhone',
       'city': 'NewYork',
       '01/09': 200,
       '02/09': '',
       '03/09': 120,
       '04/09': ''

   },
   {
       'phone': 'iPhone',
       'city': 'Los Angels',
       '01/09': 80,
       '02/09': 140,
       '03/09': '',
       '04/09': 15
   },
   {
        'phone': 'Samsung',
        'city': 'New York',
        '01/09': '',
        '02/09': 70,
        '03/09': 10
        '04/09': 20
   }
],
dynamicKeys = ['01/09', '02/09', '03/09'];

Here i need to group the data with city and sum the values of dynamicKeys, like this

[
   {
     'city': 'New York',
     '01/09': 200,
     '02/09': 70,
     '03/09': 130
   },
   {
     'city': 'Los Angels',
     '01/09': 80,
     '02/09': 140,
     '03/09': ''
 
   }
]

this is the code i tried, but its not working

let newArr = salesData.reduce((acc, item) => {
   let existItem = acc.find(d => 
      item.city === d.city
   )
   if(existItem){
     dynamicKeys.forEach(d => {
        if(!isNaN(item[d])) existItem[d] += item[d]
     })
   }else{
     acc.push(item);
   }
   return acc
},[])

here the grouping seems to be working, but the sum of dynamicKeys is not correct, what is the best way to achieve this using loadsh / plain js

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

0

We can create the desired structure using Array.reduce(), we create an object with properties for each city name, then properties for each sales date in dynamic keys.

Once we have built this object, we can use Object.values() to get the data as an array:

const salesData = [ { 'phone': 'iPhone', 'city': 'New York', '01/09': 200, '02/09': '', '03/09': 120, '04/09': ''  }, { 'phone': 'iPhone', 'city': 'Los Angeles', '01/09': 80, '02/09': 140, '03/09': '', '04/09': 15 }, { 'phone': 'Samsung', 'city': 'New York', '01/09': '', '02/09': 70, '03/09': 10, '04/09': 20 } ]; 
const dynamicKeys = ['01/09', '02/09', '03/09'];

const result = Object.values(salesData.reduce((acc, salesItem) => { 
    return dynamicKeys.reduce((acc, key) => {
       acc[salesItem.city] = acc[salesItem.city] || { city: salesItem.city }; 
       acc[salesItem.city][key] = (+acc[salesItem.city][key] || 0) + (+salesItem[key] || 0);
       return acc;
    }, acc);
}, {}))

console.log('Sales data:', result)

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!