Q:

how to iterate through an array in javascript

array = [ 1, 2, 3, 4, 5, 6 ]; 
for (index = 0; index < array.length; index++) { 
    console.log(array[index]); 
} 
10
const array = ["one", "two", "three"]
array.forEach(function (item, index) {
  console.log(item, index);
});
4
var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);
    //Do something
}
9
array = [ 1, 2, 3, 4, 5, 6 ]; 
   //set variable//set the stop count // increment each loop
for (let i = 0; i < array.length ;i++) { 
  
         array[i] //iterate through each index.
   //add the intructions you would like to perform.
    
             // add any method to array[
} 
2
let array = [ 1, 2, 3, 4 ]; //Your array

for( let element of array ) {
	//Now element takes the value of each of the elements of the array
	//Do your stuff, for example...
  	console.log(element);
}
1
let arbitraryArr = [1, 2, 3];
// below I choose let, but var and const can also be used 
for (let arbitraryElementName of arbitraryArr) {
  console.log(arbitraryElementName);
}
1
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt = txt + value + "<br>";
}
0
for (var key in validation_messages) {
    // skip loop if the property is from prototype
    if (!validation_messages.hasOwnProperty(key)) continue;

    var obj = validation_messages[key];
    for (var prop in obj) {
        // skip loop if the property is from prototype
        if (!obj.hasOwnProperty(prop)) continue;

        // your code
        alert(prop + " = " + obj[prop]);
    }
}
1
foreach ($objects as $obj) {
   echo $obj->property;
}
1

New to Communities?

Join the community