Array of objects vs Object of Objects

This is a totally subjective question. There might be many other points, but let me point out a few below:

JSON based approach:
If I am not wrong then this will be implemented using Map on server side.

Advantage: In JavaScript you can directly use users.id1, users.id2, i.e. no need of iteration

Disadvantage: On the client side, somehow you will need the ids to be present in your JSON, i.e. either hard coding them or using some dynamic approach which will tell you which id is present in your JSON.


Array Based approach: If I am not wrong then this will be implemented using Array/List on server side.

Advantage:

  1. On client side, you can directly iterate through array, without
    worrying in advance about which id is present inside it i.e. no hard
    coding.
  2. As pointed out by @JBNizet, the array based approach will maintain the order.

Disadvantage: If you want to fetch single id then you will need to iterate through the array.

Generally we don’t send much information on the client side, so array based approach will not create any problem. And transforming an array into a map is possible on both sides (server and client) if you want an id based approach.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)