1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1var items = ["item1", "item2", "item3"]
2var copie = [];
3
4items.forEach(function(item){
5 copie.push(item);
6});
1const array1 = ['a', 'b', 'c'];
2
3array1.forEach(element => console.log(element));
1///Simple One
2var a = ["a", "b", "c"];
3a.forEach(function(entry) {
4 console.log(entry);
5});
6
7
8///Function concept
9
10var fruits = ["apple", "orange", "cherry"];
11fruits.forEach(myFunction);
12
13function myFunction(item, index) {
14 document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
15}
16
1let names = ['josh', 'joe', 'ben', 'dylan'];
2// index and sourceArr are optional, sourceArr == ['josh', 'joe', 'ben', 'dylan']
3names.forEach((name, index, sourceArr) => {
4 console.log(color, idx, sourceArr)
5});
6
7// josh 0 ['josh', 'joe', 'ben', 'dylan']
8// joe 1 ['josh', 'joe', 'ben', 'dylan']
9// ben 2 ['josh', 'joe', 'ben', 'dylan']
10// dylan 3 ['josh', 'joe', 'ben', 'dylan']
1const avengers = ['IronMan','thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})