Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to group by and sum an array of objects javascript?


const array = [
  {
      "data": {
          "qty": "5",
          "toy": {
              "id": 3,
          },
          "available": "yes",
      }
  },
  {
      "data": {
          "qty": "5",
          "toy": {
              "id": 10,
          },
          "available": "no"
      }
  },
  {
      "data": {
          "qty": "59",
          "toy": {
              "id": 10,
          },
          "available": "yes",
      }
  },
  {
      "data": {
          "qty": "5",
          "toy": {
              "id": 3,
          },
          "available": "yes",
      }
  }
]


var result = [];
array.reduce(function(res, value) {
  if (!res['data']['toy'] || !res['data']['toy']['data']) {
    res['data'] = {...value['data'] };
    result.push(res['data'])
  }
  if (res['data']['available'] === value['data']['available'] && res['data']['toy']['id'] === value['data']['toy']['id']) {
    res['data']['qty'] = parseInt(res['data']['qty']) + parseInt(value['data'].qty)
  }
  return res;
}, {'data': {}});

console.log(result)

I am working on a js project and I need a bit of help here. From the array, How to get a new array that has qty as the sum of the other qty value which data.toy.id and available same. i.e. I want the below array. My code is not working as excepted. Changes to the same or new code are also fine. Thank you.

const array = [
  {
      "data": {
          "qty": "10",
          "toy": {
              "id": 3,
          },
          "available": "yes",
      }
  },
  {
      "data": {
          "qty": "5",
          "toy": {
              "id": 10,
          },
          "available": "no"
      }
  },
  {
      "data": {
          "qty": "59",
          "toy": {
              "id": 10,
          },
          "available": "yes",
      }
  }
]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You group the array into an object, where the keys are concatenation of available and id properties and finally transform the object back to an array using Object.values.

const 
  array = [
    { data: { qty: "5", toy: { id: 3 }, available: "yes" } },
    { data: { qty: "5", toy: { id: 10 }, available: "no" } },
    { data: { qty: "59", toy: { id: 10 }, available: "yes" } },
    { data: { qty: "5", toy: { id: 3 }, available: "yes" } },
  ],
  result = Object.values(
    array.reduce((r, { data }) => {
      const k = data.available + data.toy.id;
      if (r[k]) {
        r[k].data.qty = String(Number(r[k].data.qty) + Number(data.qty));
      } else {
        r[k] = { data };
      }
      return r;
    }, {})
  );

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading