Answers for "how to loop through an array js"

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
13

loop through an array javascript

let array = ['Item 1', 'Item 2', 'Item 3'];

// Here's 4 different ways
for (let index = 0; index < array.length; index++) {
  console.log(array[index]);
}

for (let index in array) {
  console.log(array[index]);
}

for (let value of array) {
  console.log(value); // Will log value in array
}

array.forEach((value, index) => {
  console.log(index); // Will log each index
  console.log(value); // Will log each value
});
Posted by: Guest on March-25-2020
11

javascript iterate array

var txt = "";
var numbers = [45, 4, 9, 16, 25];

numbers.forEach(function(value, index, array) {
  txt = txt + value + "<br>";
});
Posted by: Guest on November-25-2019
1

javascript function loop through array

//function arrayLooper will loop through the planets array
const planets = ["Mercury", "Venus", "Earth", "Mars"];

const arrayLooper = (array) => {
  for (let i = 0; i < array.length; i++) {
    console.log(array[i]);
  }
};
arrayLooper(planets);
Posted by: Guest on September-05-2021
0

iterate through an array

var arr = [1,2,3,4,5,6,7,8];

// Uses the usual "for" loop to iterate
for(var i= 0, l = arr.length; i< l; i++){
	console.log(arr[i]);
}

console.log("========================");

//Uses forEach to iterate
arr.forEach(function(item,index){
	console.log(item);
});
Posted by: Guest on November-22-2020
0

javascript loop through array

array iteration styles illustrate programming paradigms
let newArray = [];  const theArray = [2, 4, 6, 8, 10]
// 1) imperative, explicit
for (var i = 0; i < theArray.length; i++) {  // var is mutable
    console.log(theArray[i]);                // side effect from writing to console
	newArray[i] = theArray[i] * theArray[i]  // let is mutable
}
// 2) forEach function of the Array class TIP: console.log(Array) //Does not change the array, Returns undefined
function f1(theValue, theIndex, originalArray) { numVal = numVal *  theValue }
theArray.forEach(f1);   console.log( ` creates  ${   newArray   }` );
run.addEventListener("click", function () {cpeople.forEach((element) => console.log(element.firstname));});
// 3) declarative, implicit 
function f1(v,i,a) { v=v*v }
const constArray = theArray.map(fNumbers); 
console.log( ` immutable  ${ newArray }` );
Posted by: Guest on November-19-2020

Code answers related to "how to loop through an array js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language