简体   繁体   English

访问 json 中的嵌套数组

[英]Accessing nested array in a json

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

mL/hr毫升/小时

i want to find the best way of accessing array elements in a javascript object.我想找到在 javascript 对象中访问数组元素的最佳方式。 Eg: I want to find the first faculty name & first specializations for each course.例如:我想找到每门课程的第一个教员名称和第一个专业。

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'}
                     ]
                  }
               ]
            }
         ],

      }
    ]
};

I know i can get the faculty name and specialization by我知道我可以通过以下方式获得教员名称和专业

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

use Object.assign({}, deptartment.Course) instead of department.Course使用 Object.assign({}, deptartment.Course) 而不是 Department.Course

tried to use the below code but it doesn't make much difference.尝试使用以下代码,但没有太大区别。

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

Expecting期待

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

You can try this.你可以试试这个。 There were many error in the object including spelling mistakes and formatting对象中有很多错误,包括拼写错误和格式错误

 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)

I think you meant department instead of deptartment .我觉得你的意思department ,而不是deptartment I modified a bit your JSON as it was a bit buggy:我修改了你的 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'}
                     ]
                  }
               ]
            }
         ],
      }]
}

You can use map to achieve this nesting:您可以使用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