I have a unique challenge with an array of objects that I need to flatten into a new array of objects. The original structure is as follows:
const points = [
{
highlights: [
{
title: 'Title 1',
description: 'Description 1',
x: 111,
y: 222,
},
{
title: 'Title 2',
description: 'Description 2',
x: 111,
y: 222,
},
],
width: 1108,
height: 1528,
relativePath: '/image_01.jpg',
},
{
highlights: [
{
title: 'Title 3',
description: 'Description 3',
x: 199,
y: 411,
},
{
title: 'Title 4',
description: 'Description 4',
x: 213,
y: 1132,
},
],
width: 1108,
height: 1528,
relativePath: '/image_02.jpg',
},
];
The goal is to restructure each object in the 'highlights' array to have its own index, resulting in the following flat array:
[
{
title: 'Title 1',
description: 'Description 1',
x: 111,
y: 222,
width: 1108,
height: 1528,
relativePath: '/image_01.jpg',
},
{
title: 'Title 2',
description: 'Description 2',
x: 111,
y: 222,
width: 1108,
height: 1528,
relativePath: '/image_01.jpg',
},
{
title: 'Title 3',
description: 'Description 3',
x: 111,
y: 222,
width: 1108,
height: 1528,
relativePath: '/image_02.jpg',
},
{
title: 'Title 4',
description: 'Description 4',
x: 111,
y: 222,
width: 1108,
height: 1528,
relativePath: '/image_02.jpg',
},
];
I am considering using flatMap
for this task, but I'm unsure how to preserve the other properties like width
, height
, relativePath
. Any assistance would be greatly appreciated!