I have the following array structure:
const mockData = [
{
text: 'Text1',
data: [
{ field: '1' },
{ field: '2' },
{ field: '3' },
{ field: '4' },
{ field: '5' },
{ field: '6' }
]
},
{
text: 'Text1',
data: [
{ field: '1' },
{ field: '2' },
{ field: '3' },
{ field: '4' },
]
}
];
I need to splice(0, 3) a nested data array.
Here's what I tried so far, but I need to get the same output as the input array with sliced data:
const mockData = [
{
text: 'Text1',
data: [
{ field: '1' },
{ field: '2' },
{ field: '3' },
{ field: '4' },
{ field: '5' },
{ field: '6' }
]
},
{
text: 'Text1',
data: [
{ field: '1' },
{ field: '2' },
{ field: '3' },
{ field: '4' },
]
}
];
const slicedArray = mockData.reduce((accumulator, arr) => {
const spliceData = arr.data.splice(0, 3);
accumulator.push(spliceData);
return accumulator;
}, []);
console.log(slicedArray)
What's the simplest way to achieve it? Thank you!
splicemutates the array.