Search code examples
javascriptarraysjavascript-objects

Create simple object from nested array of objects of arrays


Hi folks I have my json data like this

http://json-parser.com/24145c52

I want it to change in this shape (array of objects

  const model = [
    {
        instituteName:instituteName,
        courseName:courseName,
        studentFirstname:studentFirstname,
        studentSurname:studentSurname,
        appreciation:appreciation,
        finalGrade:finalGrade,
        behaviour:behaviour,
        completenessOfStapler:completenessOfStapler
  },
  {
        instituteName:instituteName,
        courseName:courseName,
        studentFirstname:studentFirstname,
        studentSurname:studentSurname,
        appreciation:appreciation,
        finalGrade:finalGrade,
        behaviour:behaviour,
        completenessOfStapler:completenessOfStapler
  }
  ]

Please help me in this


Solution

  • First you will have to iterate over the data

    Then interate over institue courses

    Then iterate over students assigned to those courses

    Will look something like that

    const model = [];
    
     for (let i = 0; i < jsonParserData.institutes.length; i++)
      for (let j = 0; j < jsonParserData.institutes[i].courses.length; j++)
        for (let k = 0; k < jsonParserData.institutes[i].courses[j].students.length; k++) {
          model.push({
            instituteName: jsonParserData.institutes[i].name,
            courseName: jsonParserData.institutes[i].courses[j].name,
            studentFirstname: jsonParserData.institutes[i].courses[j].students[k].firstName,
            studentSurname: jsonParserData.institutes[i].courses[j].students[k].surname,
          });
          jsonParserData.institutes[i].courses[j].students[k].attributes.map((item) => {
            switch (item.option) {
              case "apreciation":
                model[model.length - 1].apreciation = item.value;
                break;
              case "finalGrade":
                model[model.length - 1].finalGrade = item.value;
                break;
              case "behaviour":
                model[model.length - 1].behaviour = item.value;
                break;
              case "completenessOfStapler":
                model[model.length - 1].completenessOfStapler = item.value;
                break;
            }
          });
        }
    

    Or using newer syntax

    for (let institute of jsonParserData.institutes)
      for (let course of institute.courses)
        for (let student of course.students) {
          model.push({
            instituteName: institute.name,
            courseName: course.name,
            studentFirstname: student.firstName,
            studentSurname: student.surname,
          });
          student.attributes.map((item) => {
            switch (item.option) {
              case "apreciation":
                model[model.length - 1].apreciation = item.value;
                break;
              case "finalGrade":
                model[model.length - 1].finalGrade = item.value;
                break;
              case "behaviour":
                model[model.length - 1].behaviour = item.value;
                break;
              case "completenessOfStapler":
                model[model.length - 1].completenessOfStapler = item.value;
                break;
            }
          });
        }