1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1var colors = ['red', 'blue', 'green'];
2
3colors.forEach(function(color) {
4 console.log(color);
5});
1let array = ['Item 1', 'Item 2', 'Item 3'];
2
3array.forEach(item => {
4 console.log(item); // Logs each 'Item #'
5});
1const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
2
3// Iterate over fruits below
4
5// Normal way
6fruits.forEach(function(fruit){
7 console.log('I want to eat a ' + fruit)
8});
1let numbers = ['one', 'two', 'three', 'four'];
2
3numbers.forEach((num) => {
4 console.log(num);
5}); // one //two //three // four
6
1const movies = [
2{name: "A New Hope", director: "George Lucas", release: "1977-05-25", episodeID: 4},
3{name: "Attack of the Clones", director: "George Lucas", release: "2002-05-16", episodeID: 2},
4{name: "Return of the Jedi", director: "Richard Marquand", release: "1983-05-25", episodeID: 6},
5{name: "Revenge of the Sith", director: "George Lucas", release: "2005-05-19", episodeID: 3},
6{name: "The Empire Strikes Back", director: "Irvin Kershner", release: "1980-05-17", episodeID: 5},
7{name: "The Phantom Menace", director: "George Lucas", release: "1999-05-19", episodeID: 1}
8]
9movies.forEach((movies) => {
10 console.log(movies.name);
11});
12/*A New Hope
13Attack of the Clones
14Return of the Jedi
15Revenge of the Sith
16The Empire Strikes Back
17The Phantom Menace*/