user310291 user310291 - 4 months ago 8
Node.js Question

In nodeJs is there a way to loop through an array without using array size?

Let's say I have

myArray = ['item1', 'item2']


I tried

for (var item in myArray) {console.log(item)}


It prints
0
1

What I wish is to have
item1
item2

Is there any other syntax that works without using

for (var i = 0; i < myArray.length; i++)

Answer

You can use Array.forEach

var myArray = ['1','2',3,4]

myArray.forEach(function(value){
  console.log(value);
});