get index of element in foreach javascript code example
Example 1: javascript foreach index
users.forEach((user, index)=>{
console.log(index);
});
Example 2: javascript .foreach
let colors = ['red', 'blue', 'green'];
colors.forEach(function(color, idx, sourceArr) {
console.log(color, idx, sourceArr)
});
Example 3: js get index from foreach
var myArray = [123, 15, 187, 32];
myArray.forEach(function (value, i) {
console.log('%d: %s', i, value);
});
Example 4: mdn foreach
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])