Answers for "get index for in loop"

15

for of get index

for (const v of ['a', 'b', 'c']) {
  console.log(v)
}

// get index
for (const [i, v] of ['a', 'b', 'c'].entries()) {
  console.log(i, v)
}
Posted by: Guest on July-31-2020
0

use index of an array within a for loop

const 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
Posted by: Guest on February-10-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language