Question #43

How to iterate through JavaScript arrays?

Answer

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})


ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

5 events in history
Answer by Alphonsio 10/11/2020 at 08:20:28 AM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})


ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

Answer by Alphonsio 06/06/2020 at 12:18:21 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

Answer by Alphonsio 06/06/2020 at 12:17:33 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}




Answer by Alphonsio 06/06/2020 at 12:16:42 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
}

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}




Question by Alphonsio 06/06/2020 at 11:52:52 AM
How to iterate through JavaScript arrays?
# ID Query URL Count

Icons proudly provided by Friconix.