Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
This is probably a super simple question, but I have the following:
let groups = [{}, {}, {}]; for(let g of groups) { console.log(g); }
How do I get the index number of said group? Preferably without doing a count.
for-in
Alternatively use forEach():
forEach()
groups.forEach((element, index) => { // do what you want });
Add a comment
.forEach() is not a function
groups
Instead of looping through groups, you could loop through groups.entries() which returns for each element, the index and the value.
Then you can extract those value with destructuring:
let groups = [{}, {}, {}]; for(let [i, g] of groups.entries()) { console.log(g); }
Simply,
let groups = [{}, {}, {}]; for(let g of groups) { console.log(groups.indexOf(g)); }
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
for-inthat goes through indices