Answers for "javascript best way to loop through array of arrays"

32

javascript loop through array

var data = [1, 2, 3, 4, 5, 6];

// traditional for loop
for(let i=0; i<=data.length; i++) {
  console.log(data[i])  // 1 2 3 4 5 6
}

// using for...of
for(let i of data) {
	console.log(i) // 1 2 3 4 5 6
}

// using for...in
for(let i in data) {
  	console.log(i) // Prints indices for array elements
	console.log(data[i]) // 1 2 3 4 5 6
}

// using forEach
data.forEach((i) => {
  console.log(i) // 1 2 3 4 5 6
})
// NOTE ->  forEach method is about 95% slower than the traditional for loop

// using map
data.map((i) => {
  console.log(i) // 1 2 3 4 5 6
})
Posted by: Guest on December-18-2020
0

how to loop through an array

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
//loop thrugh all of the numbers of the array and print them
for(let i = 0; i < array.length; i++){
	console.log(array[i]); //print all the array data in the length of i
}

//we can create some variable that store the array data and than display it 
//using for...of

for(let data of array){
	console.log(data); //print all the data that was stored in the "data" variable
}
Posted by: Guest on May-24-2021

Code answers related to "javascript best way to loop through array of arrays"

Code answers related to "Javascript"

Browse Popular Code Answers by Language