需要通过打字稿获取数组子数组的索引?
need to get index of sub array of array by typescript?
我有一个像
这样的数组
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,
},
]
}
]
我需要从这个数组中获取索引和子数组索引。像一个数据 'ReportID: 7' 这个数组索引是 1 并且子数组索引是 0 by typescript
像这样试试:
const result = reportData.reduce((acc, el, idx) => {
el.subtasks.forEach((innerEl, innerIdx) => {
acc[innerEl.ReportID] = { arrayIdx: idx, subArrayIdx: innerIdx }
});
return acc;
}, {});
结果:
{
'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 }
}
我有一个像
这样的数组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,
},
]
}
]
我需要从这个数组中获取索引和子数组索引。像一个数据 'ReportID: 7' 这个数组索引是 1 并且子数组索引是 0 by typescript
像这样试试:
const result = reportData.reduce((acc, el, idx) => {
el.subtasks.forEach((innerEl, innerIdx) => {
acc[innerEl.ReportID] = { arrayIdx: idx, subArrayIdx: innerIdx }
});
return acc;
}, {});
结果:
{
'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 }
}