繁体   English   中英

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

做这样的事情:

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

您需要像这样调用键/属性: 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

试试..of:

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

暂无
暂无

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

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