Home > Back-end >  Create simple object from nested array of objects of arrays
Create simple object from nested array of objects of arrays

Time:06-29

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

CodePudding user response:

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;
        }
      });
    }

CodePudding user response:

You can do this by running a simple over the data like this

    // This is an empty array to store data
    const model = [];
    
    // This is the data array
    const dataArr = [];

// looping over the main array
    dataArr.map((data) => {
      let Obj = {
        instituteName: "",
        courseName: "",
        studentFirstname: "",
        studentSurname: "",
        appreciation: "",
        finalGrade: "",
        behaviour: "",
        completenessOfStapler: "",
      };
      // looping over the courses array inside main array
      data.map((course) => {
        // looping over the students array inside main course
        course.map((student) => {
          let Obj = {
            instituteName: data.name,
            courseName: course.name,
            studentFirstname: student.firstName,
            studentSurname: student.surName,
            appreciation: "",
            finalGrade: "",
            behaviour: "",
            completenessOfStapler: "",
          };
          model.push(Obj);
        });
      });
    });
  • Related