Just to get past and show, forEach .
In the case of map it could be used, however it is usually used to create a new array that results from changing the array in question.
In the case of forEach
and map
, it is called with a function, the first parameter being the "pointer", or current value, the second parameter is the index of the array in question (optional parameter ), and the third parameter (also optional) is the array itself, which can be:
students.map(function (student, index, arr) {
console.log(student.name);
console.log(student.grade);
console.log(index);
console.log(arr);
});
students.forEach(function (student, index, arr) {
console.log(student.name);
console.log(student.grade);
console.log(index);
console.log(arr);
});
In ES6 mode, in this case the optional parameters are not necessary, only to list the data would look like this, with forEach
and map
:
var students = [
{
name: 'Anna',
grade: 6
},
{
name: 'John',
grade: 4
},
{
name: 'Maria',
grade: 9
}
];
// for(var i = 0; i < students.length; i++){
// console.log(students[i].name);
// console.log(students[i].grade);
//}
students.forEach(student => {
console.log(student.name);
console.log(student.grade);
});
students.map(student => {
console.log(student.name);
console.log(student.grade);
});