I have got an object:
blocks: [
{
type: 'animal',
data: { text: 'Bark bark' }
},
{
type: 'human',
data: { text: 'Speak speak' }
},
...
]
The data:
naming does not fit my needs and I would like to rename it with the type
value. So It should look like this:
blocks: [
{
type: 'animal',
animal: { text: 'Bark bark' }
},
{
type: 'human',
human: { text: 'Speak speak' }
},
...
]
How can I achieve this with very little code of javascript? My solution does work but seems to be stupid (especially if you consider 10+ different kind of types):
const blocks = [];
oldBlocks.map(block => {
const {type, data} = block;
blocks.push({
type: type,
animal: type === "animal" ? data : undefined,
human: type === "human" ? data : undefined,
....
});
});