[英]How can I use a forEach method for or any array methods for Objects of arrays in Javascript?
I'm learning to use modern array methods on arrays, but is there a way I can use these methods on objects of arrays? 我正在学习在数组上使用现代数组方法,但是有没有办法在数组对象上使用这些方法? If not is it better to store the entire object into an array?
如果不是,最好将整个对象存储到数组中?
var course = {
name: "My Javascript Tutorial",
awesome: true,
teachers: ["Brandon", "Shane", "Mike"],
students: [
{
name: "Cliff",
computer: {
OS: "macOS",
type: "iMac"
}
},
{
name: "Arthur",
computer: {
OS: "macOS",
type: "Macbook Pro"
}
},
{
name: "Donald",
computer: {
OS: "macOS",
type: "Windows PC"
}
}
]
};
course.forEach(function(item){
console.log(course.students[item].name + 'uses a ' + course.students[item].computer.type);
})
This is the Error I'm getting. 这是我得到的错误。
TypeError: Cannot read property 'students' of undefined TypeError:无法读取未定义的属性“学生”
The array you want to iterate over is course.students
, so reference course.students
and call forEach
on it. 您要迭代的数组是
course.students
,因此请参考course.students
并在其上调用forEach
。
The first argument to forEach
will be the item being iterated over (an element of the array). forEach
的第一个参数将是被迭代的项(数组的元素)。 Because your elements are objects, reference their name
and computer
properties to log them properly: 因为您的元素是对象,所以请引用它们的
name
和computer
属性以正确记录它们:
var course = { name: "My Javascript Tutorial", awesome: true, teachers: ["Brandon", "Shane", "Mike"], students: [ { name: "Cliff", computer: { OS: "macOS", type: "iMac" } }, { name: "Arthur", computer: { OS: "macOS", type: "Macbook Pro" } }, { name: "Donald", computer: { OS: "macOS", type: "Windows PC" } } ] }; course.students.forEach(function(item){ console.log(item.name + 'uses a ' + item.computer.type); })
forEach() function call is supported only for the arrays, not for the objects. 仅数组支持forEach()函数调用,而对象不支持。
In your case, if you want to traverse the array students, then you have to first get the reference to the students array from the course object. 对于您的情况,如果要遍历学生数组,则必须首先从课程对象获取对学生数组的引用。
This is how you can proceed: 您可以通过以下方式进行操作:
course.students.forEach(function(item){
console.log(item.name + 'uses a ' + item.computer.type); })
If you want to loop through your students array inside the object then you have to use indexes in the array, hence add an i variable in the foreEach 如果要遍历对象内部的学生数组,则必须在数组中使用索引,因此在foreEach中添加一个i变量
course.students.forEach(function(item, i) {
console.log(course.students[i].name + 'uses a ' + course.students[i].computer.type);
})
Or just use the object/item and display the properties of that object in every iteration 或者只使用对象/项目并在每次迭代中显示该对象的属性
course.students.forEach(function (item) {
console.log(item.name + 'uses a ' + item.computer.type);
})
The problem is, that you are trying to use the forEach loop on a object, which is not supported. 问题是,您正在尝试在不支持的对象上使用forEach循环。 Next you have the problem that you are trying to call the student name inside of a function, where it can't find the value course directly.
接下来,您遇到的问题是,您试图在函数内部调用学生姓名,而该函数无法直接找到价值课程。
What you can do is following: 您可以执行以下操作:
course.students.forEach(function(student) {
console.log(student.name);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.