Answers for "javascript list foreach"

130

javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})
Posted by: Guest on April-27-2020
56

javascript foreach

var colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});
Posted by: Guest on June-27-2019
8

javascript foreach

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

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

javascript .foreach

let colors = ['red', 'blue', 'green'];
// idx and sourceArr optional; sourceArr == colors
colors.forEach(function(color, idx, sourceArr) {
	console.log(color, idx, sourceArr)
});
// Output:
// red 0 ['red', 'blue', 'green']
// blue 1 ['red', 'blue', 'green']
// green 2 ['red', 'blue', 'green']
Posted by: Guest on April-07-2020
0

JavaScript Array Methods .forEach()

let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.forEach(function log(day) {
  console.log(day)
})

// ES6 verzió óta lehetséges az ún. "array function" használata
// az eredmény ugyanaz lesz, mint az előző szintaxissal
days.forEach(day => {
  console.log(day)
});

// amikor a függvénytörzs egy sor, kapcsos zárójel és sortörés nélkül is
// írhatjuk a függvényt
days.forEach(day => console.log(day));
Posted by: Guest on May-07-2021
0

javascript array for each

var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);

function myFunction(item, index) {
  document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
Posted by: Guest on June-29-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language