Answers for "javascript iterate through array with for loop"

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
6

loop through array javascript

/* ES6 */
const cities = ["Chicago", "New York", "Los Angeles"];
cities.map(city => {
	console.log(city)
})
Posted by: Guest on January-04-2020
0

javascript best way to iterate over array

[1,2,3,4,"df"].forEach((value, index) => console.log(index,value));
output
0 1
1 2
2 3
3 4
4 'df'
Posted by: Guest on April-04-2021
0

how to cycle through an array js

// Requiring the lodash library 
// src: https://lodash.com/docs/4.17.15#forEach

// console:  npm i --save lodash
const _ = require("lodash")

// Use of _.forEach() method

_.forEach([1, 2], function(value) {
  console.log(value)
});
// => Logs `1` then `2`.

// traversing an object
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  console.log(key)
})
// => Logs 'a' then 'b' (iteration order is not guaranteed).

//////////////// To not use lodash ////////////////
// src: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

function logArrayElements(element, index, array) {
    console.log("a[" + index + "] = " + element)
}
[2, 5, 9].forEach(logArrayElements)
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
Posted by: Guest on December-03-2020
0

javascript loop through array

var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);aegweg
    //Do something
}
Posted by: Guest on January-30-2021
0

javascript loop through arra

int[] objects = {
	"1", "2", "3", "4", "5"
};

for (int i = 0; i < objects.length; i++) {
	System.out.println(objects[i]);
}
Posted by: Guest on May-18-2021

Code answers related to "javascript iterate through array with for loop"

Code answers related to "Javascript"

Browse Popular Code Answers by Language