js for loop array method code example
Example 1: loop array javascript
var colors = ['red', 'green', 'blue'];
colors.forEach((color, colorIndex) => {
console.log(colorIndex + ". " + color);
});
Example 2: how to cycle through an array js
const _ = require("lodash")
_.forEach([1, 2], function(value) {
console.log(value)
});
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key)
})
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element)
}
[2, 5, 9].forEach(logArrayElements)