I’m running a forEach
loop using document.querySelectorAll()
, and I need to get the index of each node. What would be the best way to do this?
2
index
is always the second parameter in a forEach
loop.
const elements = document.querySelectorAll('your-selector');
elements.forEach((element, index) => {
console.log(`Element: ${element}, Index: ${index}`);
// Your code here
});