1const array1 = ['a', 'b', 'c'];
2
3array1.forEach((element) => {
4 console.log(element)
5});
6
7// expected output: "a"
8// expected output: "b"
9// expected output: "c"
1const iterable = [...];
2for (const [index, elem] in iterable.entries()) {
3 f(index, elem);
4}
5
6// or
7iterable.forEach((elem, index) => {
8 f(index, elem);
9});
1users.forEach((user, index)=>{
2 console.log(index); // Prints the index at which the loop is currently at
3});
1var items = ["item1", "item2", "item3"]
2var copie = [];
3
4items.forEach(function(item){
5 copie.push(item);
6});
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});
6// Output:
7// red 0 ['red', 'blue', 'green']
8// blue 1 ['red', 'blue', 'green']
9// green 2 ['red', 'blue', 'green']
10
1const array1 = ['a', 'b', 'c'];
2
3array1.forEach((element, index) => console.log(element, index));