Ranjith M
Ranjith M

Reputation: 247

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

Upvotes: 1

Views: 311

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30685

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)

Upvotes: 2

Related Questions