js foreach last element code example
Example 1: javascript get last object in foreach loop
arr = [1, 2, 3];
arr.forEach(function(i, idx, array){
if (idx === array.length - 1){
console.log("Last callback call at index " + idx + " with value " + i );
}
});
Example 2: javascript loop last index
for (const [i, value] of arr.entries()) {
if (i === arr.length - 1) {
// do your thing
}
}