xxxxxxxxxx
const fruits = ['apple', 'orange', 'banana', 'mango'];
fruits.forEach((item, index)=>{
console.log(index, item)
});
xxxxxxxxxx
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
xxxxxxxxxx
let arr = [1,2,3]
// forEach accepts a function, and each value in array is passed to the
// function. You define the function as you would any regular
// function, you're just doing it inside the forEach loop
arr.forEach(function(value){
console.log(value)
})
// you also have access to the index and original array:
arr.forEach(function(value, idx, array){
console.log(idx, value, array)
})
// it's common for the arrow notation to be used: it's effectively
// the same thing but the function keyword is removed and the syntax is
// a bit different
arr.forEach((value) => {
console.log(value)
})
xxxxxxxxxx
var colors = ['red', 'blue', 'green'];
colors.forEach(function(color) {
console.log(color);
});
xxxxxxxxxx
const products = [
{ name: 'Laptop', price: 32000, brand: 'Lenovo', color: 'Silver' },
{ name: 'Phone', price: 700, brand: 'Iphone', color: 'Golden' },
{ name: 'Watch', price: 3000, brand: 'Casio', color: 'Yellow' },
{ name: 'Aunglass', price: 300, brand: 'Ribon', color: 'Blue' },
{ name: 'Camera', price: 9000, brand: 'Lenovo', color: 'Gray' },
];
products.forEach(product => console.log(product.name));
//Output: Laptop Phone Watch Aunglass Camera
xxxxxxxxxx
var stringArray = ["first", "second"];
myArray.forEach((string, index) => {
var msg = "The string: " + string + " is in index of " + index;
console.log(msg);
// Output:
// The string: first is in index of 0
// The string: second is in index of 1
});
xxxxxxxxxx
let numbers = ['one', 'two', 'three', 'four'];
numbers.forEach((num) => {
console.log(num);
}); // one //two //three // four
xxxxxxxxxx
Array.forEach() is a method in JavaScript that is used to iterate over the elements of an array. The method takes a callback function as an argument, which is executed once for each element in the array. The callback function takes three arguments: the current element, the index of the current element, and the array being traversed.
Here is an example of how to use Array.forEach():
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(element, index, array) {
console.log(element, index, array);
});
This will output:
1 0 [1, 2, 3, 4, 5]
2 1 [1, 2, 3, 4, 5]
3 2 [1, 2, 3, 4, 5]
4 3 [1, 2, 3, 4, 5]
5 4 [1, 2, 3, 4, 5]
In this example, the callback function takes three parameters, the current element, the index of the current element and the array being traversed. Here the function will be called for each element in the array and the element, index, and array are passed as arguments to the callback function.
xxxxxxxxxx
const myArray = ['hello', 1, 'thanks', 5];
myArray.forEach((item, index)=>{
console.log(index, item)
})
xxxxxxxxxx
let colors = ['red', 'blue', 'green'];
// idx and sourceArr optional; sourceArr == colors
colors.forEach(function(color, idx, sourceArr) {
console.log(color, idx, sourceArr)
});
// Output:
// red 0 ['red', 'blue', 'green']
// blue 1 ['red', 'blue', 'green']
// green 2 ['red', 'blue', 'green']
xxxxxxxxxx
const numbers = [28, 77, 45, 99, 27];
numbers.forEach(number => {
console.log(number);
});