遍历 JavaScript 对象

Iterating through a JavaScript Object

访问对象时遇到问题。它们打印为未定义。帮助!我需要代码来打印学生姓名。

let students = [
        {name: 'Remy', cohort: 'Jan'},
        {name: 'Genevieve', cohort: 'March'},
        {name: 'Chuck', cohort: 'Jan'},
        {name: 'Osmund', cohort: 'June'},
        {name: 'Nikki', cohort: 'June'},
        {name: 'Boris', cohort: 'June'}
    ];



    function objPrint() {
        for (var i=0; i<students.length; i++) {
           console.log("Name: " + students[i][0] + " Cohort: " + students[i][1])
        }
    }

您正在像访问数组一样访问项目。但实际上它是一个对象数组。

顶层,已经获取到当前项的项,只需访问带点或括号表示法的键即可获取值。

let students = [
        {name: 'Remy', cohort: 'Jan'},
        {name: 'Genevieve', cohort: 'March'},
        {name: 'Chuck', cohort: 'Jan'},
        {name: 'Osmund', cohort: 'June'},
        {name: 'Nikki', cohort: 'June'},
        {name: 'Boris', cohort: 'June'}
    ];
    
    students.forEach((item) => {
       //console.log(`Name - ${item.name} :: Cohort - ${item.cohort}`);
       console.log('Name - ' + item.name + " :: Cohort - " + item.cohort );
    });

您需要像这样调用 key/attribute :students[i].name 然后方法 objPrint() 来打印值。

let students = [
        {name: 'Remy', cohort: 'Jan'},
        {name: 'Genevieve', cohort: 'March'},
        {name: 'Chuck', cohort: 'Jan'},
        {name: 'Osmund', cohort: 'June'},
        {name: 'Nikki', cohort: 'June'},
        {name: 'Boris', cohort: 'June'}
    ];



    function objPrint() {
        for (var i=0; i<students.length; i++) {
           console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort)
        }
    }
    
    objPrint();

对象的属性是通过点符号访问的,而不是带数字的括号。 所以你应该这样做

students[i].name

做这样的事情:

let students = [
    {name: 'Remy', cohort: 'Jan'},
    {name: 'Genevieve', cohort: 'March'},
    {name: 'Chuck', cohort: 'Jan'},
    {name: 'Osmund', cohort: 'June'},
    {name: 'Nikki', cohort: 'June'},
    {name: 'Boris', cohort: 'June'}
];



function objPrint() {
    for (var i=0; i<students.length; i++) {
       // Can also use students[i]['name'] , students[i]['cohort']
       // using lodash.js _.get(students, [i, 'name'], 'default value');
       // using new destructuring let {name, cohort} = students[i] then console.log("name: "+ name + " Cohort: "+cohort);
       console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort);
    }
}

尝试...的:

let students = [
    {name: 'Remy', cohort: 'Jan'},
    {name: 'Genevieve', cohort: 'March'},
    {name: 'Chuck', cohort: 'Jan'},
    {name: 'Osmund', cohort: 'June'},
    {name: 'Nikki', cohort: 'June'},
    {name: 'Boris', cohort: 'June'}
]
// returns an object as a student
for(let student of students) {
 console.log(`Name: ${student.name} Cohort: ${student.cohort}`)
}
   
因此你可以访问对象的属性,你可以做任何事情。

这种方法的好处

  • .foreach() 不同,它适用于 break、continue 和 return
  • 它避免了 for-in 的所有陷阱(使用索引而不是对象)

还有..使用反引号来避免老式的字符串连接方式 - 它看起来更好 :)

试试这个:

let students = [
        {name: 'Remy', cohort: 'Jan'},
        {name: 'Genevieve', cohort: 'March'},
        {name: 'Chuck', cohort: 'Jan'},
        {name: 'Osmund', cohort: 'June'},
        {name: 'Nikki', cohort: 'June'},
        {name: 'Boris', cohort: 'June'}
    ];

function sobjPrint() {
  for (var i in students) {

    if (students.hasOwnProperty(i)) {
       console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort)
    }
  }
}