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
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
xxxxxxxxxx
var colors = ['red', 'blue', 'green'];
colors.forEach(function(color) {
console.log(color);
});
xxxxxxxxxx
let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
console.log(word);
});
// one
// two
// three
// four
xxxxxxxxxx
var items = ["item1", "item2", "item3"]
var copie = [];
items.forEach(function(item){
copie.push(item);
});
xxxxxxxxxx
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
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
var array = ["a","b","c"];
// example 1
for(var value of array){
console.log(value);
value += 1;
}
// example 2
array.forEach((item, index)=>{
console.log(index, item)
})
xxxxxxxxxx
const avengers = ['IronMan','thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
xxxxxxxxxx
let numbers = ['one', 'two', 'three', 'four'];
numbers.forEach((num) => {
console.log(num);
}); // one //two //three // four