简体   繁体   English

从数组对象的嵌套数组创建简单对象

[英]Create simple object from nested array of objects of arrays

Hi folks I have my json data like this大家好,我有这样的json数据

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请帮助我

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM