Imagine a scenario where the data needs to be manipulated into a specific format called 'result' for easier display on the user interface. In this 'result', the month numbers are used as keys, representing each month's quantity.
const data = [
{ date: '10/1/2021', quantity: 47 },
{ date: '11/1/2021', quantity: 58 },
{ date: '12/1/2021', quantity: 96 },
{ date: '1/1/2022', quantity: 88 },
{ date: '2/1/2022', quantity: 90 },
];
const result = [
{ year: 2021, 10: 47, 11: 58, 12: 96 },
{ year: 2022, 1: 88, 2: 90 }
];
I've managed to generate an 'intermediate' structure from the original data but I'm struggling with converting it into the final 'result' format efficiently using ES6 methods.
const data = [
{ date: '10/1/2021', quantity: 47 },
{ date: '11/1/2021', quantity: 58 },
{ date: '12/1/2021', quantity: 96 },
{ date: '1/1/2022', quantity: 88 },
{ date: '2/1/2022', quantity: 90 },
];
const intermediate = data.map(o => {
// eslint-disable-next-line no-unused-vars
const [month, day, year] = o.date.split('/'); // destructuring assignment
return { year: year, [month]: o.quantity }
});
console.log(intermediate);