简体   繁体   English

如何在Javascript中使用forEach方法或数组对象的任何数组方法?

[英]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: 因为您的元素是对象,所以请引用它们的namecomputer属性以正确记录它们:

 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.

相关问题 如何对JavaScript中数组中的对象使用forEach方法? - How to use forEach method for objects in array in JavaScript? 如何在Javascript中创建自定义对象数组的数组? - How can I make an array of arrays of custom objects in Javascript? 我怎样才能在javascript中使用forEach for Json Array - How can i use forEach for Json Array in javascript JavaScript:如何合并这两个不完整对象的 arrays 并制作完整对象的数组 - JavaScript: how can I merge these two arrays of incomplete objects and make an array of complete objects 像javascript中的对象一样在数组上应用forEach方法 - apply forEach method on array like objects in javascript Javascript ForEach 在 Arrays 的数组上 - Javascript ForEach on Array of Arrays 如何在 JavaScript 中解构数组数组? - How can I destructure an array of arrays in JavaScript? 如何为存储在JavaScript数组中的对象创建相同的属性和方法? - How can I create identical properties and methods to objects stored in an array in JavaScript? 如何使用JavaScript Array fill()方法同时声明并填充数组(矩阵)的数组? - How can I declare and fill at the same time an array of arrays (matrix) using the JavaScript Array fill() Method? 请我希望在我的JavaScript数组数组上使用reduce方法 - Please I wish to use the reduce method on my javascript array of arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM