Hi!🙋♂️
I wrote this article for show you this 3 forEach ways:
Take this simply array:
var array = [
{
name: 'John'
},
{
name: 'Mary'
}
];
If you want to cycle the element with forEach() method, you have three ways:
Keys:
Object.keys
Object.keys(array).forEach(function(key) {
console.log(key); //--> 0 1
});
Values:
Object.values
Object.values(array).forEach(function(value) {
console.log(value); //--> name: 'John' name: 'Mary'
console.log(value.name); //--> John Mary
});
forEach:
.forEach
array.forEach(function (val) {
console.log(val); //--> name: 'John' name: 'Mary'
})
Top comments (0)