Answers for "javascript how to get index number of an element in the array in for loop"

5

js for of array with index

for (const [index, value] of [1, 2, 3, 4, 5].entries()) {
  console.log(index, value);
}
Posted by: Guest on June-18-2021
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 how to get index number of an element in the array in for loop"

Code answers related to "Javascript"

Browse Popular Code Answers by Language