I am faced with transforming an array of Objects that contain a nested structure. Here is an example of the data:
[
{
geography: 'Austia',
product: 'RTD Coffee',
dataType: 'Off-Trade rsp (curr/con, local)',
timeSeries: [
{
year: 2017,
value: 0.148891823777856,
highlight: 1,
},
{
year: 2018,
value: 0.148965642232877,
highlight: 1,
},
{
year: 2019,
value: 0.149039460687898,
highlight: 1,
},
{
year: 2020,
value: 0.149113279142919,
highlight: 1,
},
{
year: 2021,
value: 0.149187097597941,
highlight: 1,
},
{
year: 2022,
value: 0.149260916052962,
highlight: 1,
},
],
},...
];
My goal is to transform this data into a new pattern where the TimeSeries array objects' properties are extracted and mapped to the top level like this:
[
{
geography: 'Austria',
product: 'RTD Coffee',
dataType: 'Off-Trade rsp (curr/con, local)',
2017: 0.148891823777856,
2018: 0.148965642232877,
2019: 0.149039460687898,
2020: 0.149113279142919,
2021: 0.149187097597941,
2022: 0.149260916052962,
},
]
Does anyone know how I can achieve this transformation?