I've been charged with creating a simple data source so clients can retrieve a list of things by JSON. Each thing has an ID, so my first impulse was to create something like
{
"13": {
"name": "foo",
"height": 17
},
"18": {
"name": "bar",
"height": 22
}
...
}
But I've been told that this is an abuse of JS properties as an associative array, so that something like this would be more appropriate:
[
{
"id": 13,
"name": "foo",
"height": 17
},
{
"id": 18,
"name": "bar",
"height": 22
}
]
The second version just seems... difficult. What's the best practice here?
If you want to access the object via the ID, use the former variant with the ID as property name. Then you can directly access that object using the object’s ID. Otherwise, if you need to work with all objects anyway, use the latter variant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With