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.
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.
Alternatively use forEach()
:
groups.forEach((element, index) => {
// do what you want
});
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));
}