Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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;
        }
      });
    }
about 4 years ago · Juan Pablo Isaza Report

0

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);
        });
      });
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!