Answers for "fore each"

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
8

forEach

const arraySparse = [1,3,,7]
let numCallbackRuns = 0

arraySparse.forEach((element) => {
  console.log(element)
  numCallbackRuns++
})

console.log("numCallbackRuns: ", numCallbackRuns)

// 1
// 3
// 7
// numCallbackRuns: 3
// comment: as you can see the missing value between 3 and 7 didn't invoke callback function.
Posted by: Guest on April-18-2020
3

for each javascript

const movies = [
{name: "A New Hope", director: "George Lucas", release: "1977-05-25", episodeID: 4},
{name: "Attack of the Clones", director: "George Lucas", release: "2002-05-16", episodeID: 2},
{name: "Return of the Jedi", director: "Richard Marquand", release: "1983-05-25", episodeID: 6},
{name: "Revenge of the Sith", director: "George Lucas", release: "2005-05-19", episodeID: 3},
{name: "The Empire Strikes Back", director: "Irvin Kershner", release: "1980-05-17", episodeID: 5},
{name: "The Phantom Menace", director: "George Lucas", release: "1999-05-19", episodeID: 1}     
]
movies.forEach((movies) => {
  console.log(movies.name);
});
/*A New Hope
Attack of the Clones
Return of the Jedi
Revenge of the Sith
The Empire Strikes Back
The Phantom Menace*/
Posted by: Guest on November-07-2020
3

for each

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"
Posted by: Guest on April-08-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language