javascript using forEach to create array code example
Example 1: javascript foreach
let numbers = ['one', 'two', 'three', 'four'];
numbers.forEach((num) => {
console.log(num);
}); // one //two //three // four
Example 2: javascript foreach object
function logArrayElements(element, index, array) {
console.log('a[' + index + '] = ' + element)
}
// Notice that index 2 is skipped, since there is no item at
// that position in the array...
[2, 5, , 9].forEach(logArrayElements)
// logs:
// a[0] = 2
// a[1] = 5
// a[3] = 9