1

i have a json like this:

 [{
          "semester":"1",
          "classification":"Excellent",
          "schoolYear":"2018"
       },
       {
          "semester":"2",
          "classification":"Intermediate",
          "schoolYear":"2018"
       },
       {
          "semester":"1",
          "classification":"Excellent",
          "schoolYear":"2018"
       },
       {
          "semester":"2",
          "classification":"Good",
          "schoolYear":"2017"
       },
       {
          "semester":"1",
          "classification":"Excellent",
          "schoolYear":"2017"
       },
       {
          "semester":"2",
          "classification":"Good",
          "schoolYear":"2017"
       },
       {
          "semester":"1",
          "classification":"Excellent",
          "schoolYear":"2017"
       },
       {
          "semester":"2",
          "classification":"Fail",
          "schoolYear":"2017"
       },
       {
          "semester":"1",
          "classification":"Good",
          "schoolYear":"2017"
       }]

and i want the output following this format by a function:

[
  {
    schoolYear: '2018',
    semesters: [
      {
        Excellent: 2,
        Good: 0,
        Intermediate: 0,
        Average: 0,
        Weak: 0,
        Fail: 0
      },
      {
        Excellent: 0,
        Good: 0,
        Intermediate: 1,
        Average: 0,
        Weak: 0,
        Fail: 0
      }
    ]
  },
  {
    schoolYear: '2017',
    semesters: [
      {
        Excellent: 3,
        Good: 1,
        Intermediate: 0,
        Average: 0,
        Weak: 0,
        Fail: 0
      },
      {
        Excellent: 0,
        Good: 2,
        Intermediate: 0,
        Average: 0,
        Weak: 0,
        Fail: 1
      }
    ]
  }
]

as you can see, the output is an array, each element in the array has 2 attributes, the schoolYear is a String, and the semesters is an array which contains 2 elements only, the first element at index 0 is for the "semester": "1" according to the input and the second element at index 1 is for the "semester": "2", of course.

i want to say thank to you to take time help me out, you can modify the code in this codesandbox demo here, once again, thank you very much and have a good day

0

1 Answer 1

1

Here is an working example:

var obj = [
    {
        semester: "1",
        classification: "Excellent",
        schoolYear: "2018"
    },
    {
        semester: "2",
        classification: "Intermediate",
        schoolYear: "2018"
    },
    {
        semester: "1",
        classification: "Excellent",
        schoolYear: "2018"
    },
    {
        semester: "2",
        classification: "Good",
        schoolYear: "2017"
    },
    {
        semester: "1",
        classification: "Excellent",
        schoolYear: "2017"
    },
    {
        semester: "2",
        classification: "Good",
        schoolYear: "2017"
    },
    {
        semester: "1",
        classification: "Excellent",
        schoolYear: "2017"
    },
    {
        semester: "2",
        classification: "Fail",
        schoolYear: "2017"
    },
    {
        semester: "1",
        classification: "Good",
        schoolYear: "2017"
    }
];
const formatArray = (oldArray) => {

    const newArray = [];
    oldArray.forEach((oldEntry) => {
        let newObjIndex = newArray.findIndex((e) => e.schoolYear === oldEntry.schoolYear);
        if (newObjIndex === -1) {
            newArray.push({
                schoolYear: oldEntry.schoolYear
            });
            newObjIndex = newArray.length - 1;
        }
        if (!newArray[newObjIndex].semester) {
            newArray[newObjIndex].semester = [];
        }
        if (!newArray[newObjIndex].semester[oldEntry.semester]) {
            newArray[newObjIndex].semester[oldEntry.semester] = {};
        }
        newArray[newObjIndex].semester[oldEntry.semester][oldEntry.classification] = newArray[newObjIndex].semester[oldEntry.semester][oldEntry.classification]
            ? newArray[newObjIndex].semester[oldEntry.semester][oldEntry.classification] + 1
            : 1;
    });
    return newArray;
};
console.log(formatArray(obj));

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for the help, this is for my graduation thesis, you have just boosted my progress to finish it faster, lots of love and many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.