Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sum & group object data?

As stated above, how do I sum & group object data?
I try to use forEach & Object.key() to group data.
But I'm not too familiar with it.
This is my example data :

const srcArr =[
    {
        "1" : {
            "60" : {
                "point" : 10,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },
    {
        "2" : {
            "60" : {
                "point" : 100,
                "count" : 2
            }
        }
    },
    {
        "1" : {
            "88" : {
                "point" : 50,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },

I want data like this :

{ 
  '1': { '60': { money: 10, count: 1 }, 
         '68': { money: 40, count: 2 }, 
         '88': { money: 50, count: 1 }},
  '2': { '60': { money: 100, count: 2 }}
}
like image 394
Benji Avatar asked Mar 02 '26 06:03

Benji


2 Answers

Try this

cc = [
    {
        "1" : {
            "60" : {
                "point" : 10,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },
    {
        "2" : {
            "60" : {
                "point" : 100,
                "count" : 2
            }
        }
    },
    {
        "1" : {
            "88" : {
                "point" : 50,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    }
]
const result = cc.reduce((arr, o) =>{
    let k = Object.keys(o)[0];
   arr[k] = arr[k] || {};
   let opo = o[k];
   if(arr[k]) {
    Object.keys(arr[k]).map(kk => {
      Object.keys(o[k]).map(cc => {
        if(kk === cc) {
          opo[cc] = opo[cc] || {};
          opo[cc].count =   o[k][cc].count + arr[k][kk].count;
          opo[cc].point =   o[k][cc].point + arr[k][kk].point;
        }
      });
   });
   }        
   arr[k] = {...arr[k], ...opo};    
   return arr;
}, {});

console.log(result);

May be this is what you are expecting. Ignore the keys naming convention :p

like image 161
Qubaish Bhatti Avatar answered Mar 03 '26 18:03

Qubaish Bhatti


I would use Array.reduce

const srcArr = [{
    "1": {
      "60": {
        "point": 10,
        "count": 1
      },
      "68": {
        "point": 20,
        "count": 1
      },
    }
  },
  {
    "2": {
      "60": {
        "point": 100,
        "count": 2
      }
    }
  },
  {
    "1": {
      "88": {
        "point": 50,
        "count": 1
      },
      "68": {
        "point": 20,
        "count": 1
      },
    }
  }
]

let res = srcArr.reduce((arr, o) => {
  // get the key: 1,2, or 3 etc
  let k = Object.keys(o)[0];
  // create a new object with that key if it does not exist
  arr[k] = arr[k] || {};
  // push the objects
  arr[k] = Object.assign(arr[k], o[k]);
  return arr;
}, {});

console.log(res);
like image 42
Adam Azad Avatar answered Mar 03 '26 20:03

Adam Azad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!