1let words = ['one', 'two', 'three', 'four'];
2words.forEach((word) => {
3 console.log(word);
4});
5// one
6// two
7// three
8// four
1var items = ["item1", "item2", "item3"]
2var copie = [];
3
4items.forEach(function(item){
5 copie.push(item);
6});
1var array = ["a","b","c"];
2// example 1
3 for(var value of array){
4 console.log(value);
5 value += 1;
6 }
7
8// example 2
9 array.forEach((item, index)=>{
10 console.log(index, item)
11 })
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']
1let dogs = ['Husky','Black Lab','Australian Shepard','Golden Retriever'];
2
3dogs.forEach(function(dog) {
4 console.log(dog);
5});
1let colors = ['red', 'blue', 'green'];
2// idx and sourceArr optional; sourceArr == colors
3colors.forEach(function(color, idx, sourceArr) {
4 console.log(color, idx, sourceArr)
5});
1const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
2
3fruits.forEach(function(fruit){
4 console.log('I want to eat a ' + fruit)
5});