0

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

4 Answers 4

1

The array you want to iterate over is course.students, so reference course.students and call forEach on it.

The first argument to forEach will be the item being iterated over (an element of the array). Because your elements are objects, reference their name and computer properties to log them properly:

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

Sign up to request clarification or add additional context in comments.

Comments

1

forEach() function call is supported only for the arrays, not for the objects.

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

2 Comments

please also insert the last key and parenthesis '})' in the code snippet, thanks
Done :) @borchvm
0

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

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

Comments

0

The problem is, that you are trying to use the forEach loop on a object, which is not supported. 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); 
});

1 Comment

excellent response thank you, allowed me to write course.students.forEach(function(student){ console.log(student.name + 'uses a ' + student.computer.type); });

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.