I have a function that returns me an array of objects. How do I manipulate the data to return me an object of objects?
Below is my example:
const includedStates = ['NJ', 'NY'];
const withoutMap = () => {
return {
"NJ": {
fill: "red",
clickHandler: (event) => console.log('Custom handler for NJ', event.target.dataset.name)
},
"NY": {
fill: "red",
clickHandler: (event) => console.log('Custom handler for NY', event.target.dataset.name)
}
};
};
const withMap = () => {
return includedStates.map(item => {
return {
[item]: 'red',
clickHandler: (event) => console.log(event.target.dataset.name)
}
})
};
console.log('withoutMap =>', withoutMap());
console.log('withMap =>', withMap())
Please advice. I want the withMap function to return me the datastructure of how withoutMap returns.
The datastructure I expect is
{
"NJ": {
"fill": "red",
"clickHandler": (event) => console.log('Custom handler for NJ', event.target.dataset.name)
},
"NY": {
"fill": "red",
"clickHandler": (event) => console.log('Custom handler for NY', event.target.dataset.name)
}
}