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)
}
}
asked Sep 20, 2019 at 2:27
arunmmanoharan
2,7073 gold badges38 silver badges76 bronze badges
3 Answers 3
One option is to use forEach and construct an object as you're iterating:
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 = () => {
const result = {}
includedStates.forEach(item => {
result[item] = {
[item]: 'red',
clickHandler: (event) => console.log(event.target.dataset.name)
}
})
return result;
};
console.log('withoutMap =>', withoutMap());
console.log('withMap =>', withMap())
Another option is to use reduce:
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.reduce((result, item) => {
result[item] = {
[item]: 'red',
clickHandler: (event) => console.log(event.target.dataset.name)
}
return result;
}, {})
};
console.log('withoutMap =>', withoutMap());
console.log('withMap =>', withMap())
answered Sep 20, 2019 at 2:30
skovy
5,6502 gold badges23 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
.map() will just map the elements in the array itself, you should use .reduce() to reduce it to an object.
const includedStates = ['NJ', 'NY'];
const withReduce = () => {
return includedStates.reduce((result, item) => {
result[item] = {
fill: 'red',
clickHandler: (event) => console.log(event.target.dataset.name)
};
return result;
},{});
};
console.log('withReduce =>', withReduce())
answered Sep 20, 2019 at 2:38
Vandesh
6,9642 gold badges32 silver badges38 bronze badges
Comments
This solution uses reduce() with dynamic property names and destructuring:
const includedStates = ['NJ', 'NY'];
const withMap = () => includedStates.reduce((result, item) => ({
...result,
[item]: {
fill: 'red',
clickHandler: (event) => console.log(event.target.dataset.name)
}
}), {});
console.log('withMap =>', withMap());
answered Sep 20, 2019 at 2:38
Robby Cornelissen
98.6k23 gold badges154 silver badges180 bronze badges
Comments
lang-js