You can obtain the global index of each element by calling a method getIndex(i, j)
which accepts the two loop indexes as arguments:
<div class="group" *ngFor="let group of data; let i = index">
Header {{i + 1}}
<div *ngFor="let item of group; let j = index">
Item {{j + 1}} - index = {{ getIndex(i, j) }}
</div>
</div>
and which uses Array.reduce() as follows:
data = [
["a", "b", "c"],
["p"],
["x", "y"],
];
getIndex(outerIndex, innerIndex) {
return this.data.reduce((accumulator, currentGroup, groupIndex) => {
return accumulator + (groupIndex < outerIndex ? currentGroup.length : 0);
}, innerIndex);
}
See this stackblitz for a demo.
Here is an alternative version of getIndex(i, j)
which uses a simple for
loop:
getIndex(outerIndex, innerIndex) {
let totalIndex = innerIndex;
for (let groupIndex = 0; groupIndex < outerIndex; groupIndex++) {
totalIndex += this.data[groupIndex].length;
}
return totalIndex;
}