Answers for "for each loops javascript"

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
35

for each js

const fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

// Normal way
fruits.forEach(function(fruit){
  console.log('I want to eat a ' + fruit)
});
Posted by: Guest on March-29-2020
31

js for each item do

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

array.forEach(item => {
	console.log(item); // Logs each 'Item #'
});
Posted by: Guest on February-20-2020
1

loop an array in javascript

let array = ["loop", "this", "array"]; // input array variable
for (let i = 0; i < array.length; i++) { // iteration over input
	console.log(array[i]); // logs the elements from the current input
}
Posted by: Guest on May-18-2020
20

javascript for each loop

var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
    console.log(color);
});
Posted by: Guest on July-19-2019
14

for each javascript

let numbers = ['one', 'two', 'three', 'four'];

numbers.forEach((num) => {
  console.log(num);
});  // one   //two //three // four
Posted by: Guest on July-16-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language