xZile
xZile

Reputation: 11

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

Upvotes: 0

Views: 313

Answers (4)

Mohit Dhingra
Mohit Dhingra

Reputation: 11

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

Upvotes: 1

Julian
Julian

Reputation: 3869

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

Upvotes: 0

EugenSunic
EugenSunic

Reputation: 13703

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

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370929

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

Upvotes: 1

Related Questions