访问JSON中的嵌套数组

时间:2019-07-26 19:14:23

标签: javascript arrays json ecmascript-6 javascript-objects

让年龄=数据   .filter(isDog)   .map(dogYears)   .reduce(sum);

mL / hr

我想找到访问javascript对象中数组元素的最佳方法。 例如:我想为每门课程找到第一个教职员工名称和专业名称。

var students = 
{  
   deptartment:[  
      {  
         name:'Computer Science',
         age:20,
         Course:[  
            {  id: 100000
               name:'Object Oriented Programming',
               faculty:[  
                  {  
                     id:123,
                     name:'John',
                     Specialization: [
                       {name: 'science'},
                       {name: 'Physics'}
                     ]
                  }
                ]
            },
            {  id: 100001
               name:'C#',
               faculty:[    
                 {  
                     id:124,
                     name:'Denis', 
                     Specialization: [
                       {name: 'Ecnonomics'},
                       {name: 'Physics'}
                     ]
                  }
               ]
            }
         ],

      }
    ]
};

我知道我可以通过

获得教师姓名和专业名称
  var courses= deptartment && deptartment.Course ;
    var facultyWithSpecialization= {};
    if(courses){
      courses.forEach(course =>{
        var fname = course.faculty && course.faculty[0].name;
        var s= course.faculty && course.faculty.Specialization;
        facultyWithSpecialization[fname] = s && s[0].name;
      })
    }

使用Object.assign({},deptartment.Course)代替Department.Course

试图使用下面的代码,但是并没有太大的区别。

 var courses=Object.values(Object.assign({}, deptartment.Course));
var fname = Object.values(Object.assign({}, course.faculty[0].Specialization[0]));

期待

'John': 'science'
'Denis': 'Ecnonomics'

2 个答案:

答案 0 :(得分:1)

您可以尝试一下。对象中有很多错误,包括拼写错误和格式

string s = "\"Papua New Guinea\"";
string trimmed = s.Trim('"');

答案 1 :(得分:0)

我认为您的意思是department,而不是deptartment。 我修改了您的JSON,因为它有点问题:

var students = {  
   departments:[  
      {  
         name:'Computer Science',
         age:20,
         Courses:[  
            {  id: 100000,
               name:'Object Oriented Programming',
               faculty:[  
                  {  
                     id:123,
                     name:'John',
                     Specialization: [
                       {name: 'science'},
                       {name: 'Physics'}
                     ]
                  },
                 {  
                     id:124,
                     name:'Denis', 
                     Specialization: [
                       {name: 'Ecnonomics'},
                       {name: 'Physics'}
                     ]
                  }
               ]
            }
         ],
      }]
}

您可以使用map来实现此嵌套:

students.departments.map(
  department => department.Courses.map(
    course => course.faculty.map(
      student => ({
        name: student.name,
        specialization: student.Specialization[0].name // check nulls here!
      })
    )
  )
)