繁体   English   中英

访问 json 中的嵌套数组

[英]Accessing nested array in a json

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

毫升/小时

我想找到在 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'

你可以试试这个。 对象中有很多错误,包括拼写错误和格式错误

 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: 124, name: 'Denis', Specialization: [{ name: 'Ecnonomics' }, { name: 'Physics' } ] } ] }], }] } var obj = {}; students.deptartment.forEach((e) => { e.Course.forEach((k) => { k.faculty.forEach((l) => { obj[l.name] = l.Specialization[0].name }) }) }) console.log(obj)

我觉得你的意思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!
      })
    )
  )
)

暂无
暂无

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

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