1

Having trouble accessing objects. They are printing as undefined. Help! I need the code to print the student names.

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])
        }
    }
3
  • 1
    have you tried forEach? w3schools.com/jsref/jsref_forEach.asp Commented Oct 1, 2018 at 17:15
  • 3
    Well, students[i] is not an array, students[i] is an object containing name and cohort properties. So instead of doing students[i][0], you want to do students[i].name. Commented Oct 1, 2018 at 17:15
  • Thanks everyone! All your answers were great. Commented Oct 2, 2018 at 23:57

6 Answers 6

3

Do something like this:

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You are accessing the items as if it is an array. But in reality is it an array of objects.

The top level, already gets the item that is the current item, just access the keys with dot or bracket notation to get the value.

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

Comments

1

You need to call the key/attribute like this : students[i].name and then the method objPrint() to print the values.

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

Comments

0

Properties of objects are access by the dot notation rather than a bracket with number. So you should do it like this

students[i].name

Comments

0

Try this:

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

Comments

0

Try for..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}`)
}
   
therefore u have access to object's attributes and u can do whatever.

benefits of this approach

  • unlike .foreach() it works with break, continue, and return
  • it avoids all the pitfalls of for–in (working with indexes and not objects)

and also.. use backticks to avoid the old fashioned way of string concatenation - it looks better :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.