Вы можете попробовать манипуляции с массивом с помощью map
, reduce
.
Нижеприведенный фрагмент может помочь
const arr = {
"2020-W1": {
0: { site_id: "004", year_week: "2020-W1", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 1000 },
1: { site_id: "004", year_week: "2020-W1", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 300 },
2: { site_id: "004", year_week: "2020-W1", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 20 },
3: { site_id: "004", year_week: "2020-W1", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 100 },
},
"2020-W2": {
0: { site_id: "004", year_week: "2020-W2", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 },
1: { site_id: "004", year_week: "2020-W2", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 400 },
2: { site_id: "004", year_week: "2020-W2", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 },
3: { site_id: "004", year_week: "2020-W2", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 5000 },
},
"2020-W3": {
0: { site_id: "004", year_week: "2020-W3", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 1000 },
1: { site_id: "004", year_week: "2020-W3", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 },
2: { site_id: "004", year_week: "2020-W3", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 3000 },
3: { site_id: "004", year_week: "2020-W3", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 4000 },
},
}
const res = Object.keys(arr).map((year_week) => {
const typeAmounts = Object.keys(arr[year_week]).reduce(
(acc, index) => ({
...acc,
[arr[year_week][index].type]: arr[year_week][index].amount,
}),
{}
)
return {
year_week,
...typeAmounts,
}
})
console.log(res)