I have an array like
let reportData = [
{
ReportID: 1,
ReportHead: 'Revenue',
collection: 75,
subtasks: [
{
ReportID: 2, ReportHead: 'Plan timeline', collection: 100, isDeleted: false,
},
{
ReportID: 3, ReportHead: 'Plan budget', collection: 100, isDeleted: false,
},
{
ReportID: 4, ReportHead: 'Allocate resources', collection: 100, isDeleted: false,
},
{
ReportID: 5, ReportHead: 'Income complete', collection: 0, isDeleted: false,
}
]
},
{
ReportID: 6,
ReportHead: 'Liabilities',
subtasks: [
{
ReportID: 7, ReportHead: 'Software Specification', collection: 60, isDeleted: false,
},
{
ReportID: 8, ReportHead: 'Develop prototype', collection: 100, isDeleted: false,
},
{
ReportID: 9, ReportHead: 'Get approval from customer', collection: 100, isDeleted: false,
},
]
}
]
I need to get index and sub array index from this array. like a data 'ReportID: 7' this array index is 1 and the sub array index is 0 by typescript
Try smth like this:
const result = reportData.reduce((acc, el, idx) => {
el.subtasks.forEach((innerEl, innerIdx) => {
acc[innerEl.ReportID] = { arrayIdx: idx, subArrayIdx: innerIdx }
});
return acc;
}, {});
Result:
{
'2': { arrayIdx: 0, subArrayIdx: 0 },
'3': { arrayIdx: 0, subArrayIdx: 1 },
'4': { arrayIdx: 0, subArrayIdx: 2 },
'5': { arrayIdx: 0, subArrayIdx: 3 },
'7': { arrayIdx: 1, subArrayIdx: 0 },
'8': { arrayIdx: 1, subArrayIdx: 1 },
'9': { arrayIdx: 1, subArrayIdx: 2 }
}