I am developing a React web app. Until now, I always stored data in an array like such:
areas: [
{ id: 0, title: 'Some title', isSelected: false }
]
Then I could just do
this.state.areas.map(x => <Component key={x.id} ... />
However after going over the Redux docs, I see an advantage is storing data like so:
areas: {
'some title': { id: 0, isSelected: false }
}
This makes it super easy to update the currently selected area by doing
this.state.areas['some title].isSelected = true
As opposed to doing .filter by the id, then updating isSelected
.
However, storing using object keys instead of an array means I have to write:
{
Object.entries(this.props.areas).map(([k, v]) => {
return <Component key={k.id} ... />
})
}
Which is a lot less readable, and a lot more typing.
So my question is:
- Is there a nicer way than
Object.entries...
to achieve what I'm doing? Or is there a better way? - Should I just be using an array in the first place?
Thanks.