How can I extract elements of an array inside an array to this array?
What I have is this:
const tableData = [
{
display: '2022-03',
column: 'data',
detailList: [
{
title: 'Quantity',
value: 1,
sourceId: null,
},
{
title: 'Price',
value: 2,
sourceId: null,
},
{
title: 'Weight',
value: 3,
sourceId: null,
},
{
title: 'income',
value: 4,
sourceId: null,
},
],
},
{
display: '2022-02',
column: 'data',
detailList: [
{
title: 'Quantity',
value: 7,
sourceId: null,
},
{
title: 'Price',
value: 6,
sourceId: null,
},
{
title: 'Weight',
value: 5,
sourceId: null,
},
{
title: 'income',
value: 4,
sourceId: null,
},
],
},
];
Now I want it to become this:
res = [
{
title: '2022-03',
'Quantity': '1',
'Price': '2',
'Weight': '3',
'income': '4',
},
{
title: '2022-02',
'Quantity': '7',
'Price': '6',
'Weight': '5',
'income': '4',
},
];
What's the best way to do it?
I'm trying to create a new array outside, and use two .map() to make it. But it looks not readable.
Can somebody give me a better solution that uses some ES6 features that makes it look more readable.
tableDataarray (using.map()) and use.reduce()for thedetailListarray to return an object with the desired key-value pairs. Append this object withtitle: ..... That's it !