0

I have a json object with data, I need to group these by name, sum the weight by name, and sum the grades. I have been trying for 2 days now. It should look like {name,weightTotal,grade{grade1total:, grade2total, grade2total}}... i looked through about 30 stack qs and tried reducing and foreach groupby and I don't understand any of it, I'm a 3rd year student doing my first internship. Need this to progress on the frontend to display these as a table. These are from rest endpoint btw.

Data

"data": [
    {
      name: "france Naicin",
      avgTotalWeight: 16,
      grade: {
        grade1: 16,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "pacific gigas",
      avgTotalWeight: 16,
      grade: {
        grade1: 16,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 13,
      grade: {
        grade1: 13,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 14,
      grade: {
        grade1: 14,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 15,
      grade: {
        grade1: 15,
        grade2: 0,
        grade3: 0,
      },
    },
  ],

My code to get this json:

let a = [];

    for (let i = 0; i <= data.length; i++) {
      if (!data[i]) {
        continue;
      }

      const {avgTotalWeight} = data[i];
      const {name} = data[i].truerun.batch.shellfish;
      const {alias} = data[i].truerun.grade_list;
      const {updatedAt} = data[i];
      const {isExist} = data[i].truerun;

      if (avgTotalWeight !== null) {
        let grade1,
          grade2,
          grade3 = 0;

        if (alias === "G1") {
          grade1 = avgTotalWeight;
        } else grade1 = 0;

        if (alias === "G2") {
          grade2 = avgTotalWeight;
        } else grade2 = 0;

        if (alias === "G3") {
          grade3 = avgTotalWeight;
        } else grade3 = 0;

        let b = {
          name: name,
          avgTotalWeight: avgTotalWeight,
          //updatedAt: updatedAt,
          //isExist: isExist,
          grade: {
            grade1: grade1,
            grade2: grade2,
            grade3: grade3,
          },
        };

        a.push(b);
      }
    }

    return a;
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • Would you add what is your expected result? – DecPK May 20 '21 at 14:54
  • { "name": "france Naicin", "avgTotalWeight": 36, "grade": { "grade1": 16, "grade2": 10, "grade3": 10 } }, { "name": "pacific gigas", "avgTotalWeight": 46, "grade": { "grade1": 16, "grade2": 10, "grade3": 20 } }, – Michal Kubiak May 20 '21 at 15:01
  • sorry for the formatting, but just totals under different names so grouped by names, total weight of them, and total weight of each grades – Michal Kubiak May 20 '21 at 15:02
  • How `france Naicin`'s `avgTotalWeight` is `36`. Is it not should be `58` – DecPK May 20 '21 at 15:03
  • yea sorry there is more entries in the data I just scooped the first few objects, it should just be the total whatever the total should be! – Michal Kubiak May 20 '21 at 15:05

1 Answers1

0

You can easily achieve this result using reduce

If some case grade key increases then you can add dynamic code so that it can work with infinite keys in grade. Just replace

isExist.grade.grade1 += grade.grade1;
isExist.grade.grade2 += grade.grade2;
isExist.grade.grade3 += grade.grade3;

with

Object.keys(isExist.grade).forEach((key) => {
  isExist.grade[key] += curr.grade[key];
});

const obj = {
  data: [
    {
      name: "france Naicin",
      avgTotalWeight: 16,
      grade: {
        grade1: 16,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "pacific gigas",
      avgTotalWeight: 16,
      grade: {
        grade1: 16,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 13,
      grade: {
        grade1: 13,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 14,
      grade: {
        grade1: 14,
        grade2: 0,
        grade3: 0,
      },
    },
    {
      name: "france Naicin",
      avgTotalWeight: 15,
      grade: {
        grade1: 15,
        grade2: 0,
        grade3: 0,
      },
    },
  ],
};

const result = obj.data.reduce((acc, curr) => {
  const { name, avgTotalWeight, grade } = curr;
  const isExist = acc.find((o) => o.name === name);
  if (isExist) {
    isExist.weightTotal += avgTotalWeight;
    isExist.grade.grade1 += grade.grade1;
    isExist.grade.grade2 += grade.grade2;
    isExist.grade.grade3 += grade.grade3;
  } else {
    acc.push({
      name,
      weightTotal: avgTotalWeight,
      grade,
    });
  }
  return acc;
}, []);

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42