对象数组VS对象的对象 [英] Array of objects vs Object of Objects

查看:158
本文介绍了对象数组VS对象的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是决定权衡以下符号之间:

The issue is to decided the trade offs between following notations:

基于JSON

"users": {
    "id1": {
        "id": "id1",
        "firstname": "firstname1",
        "lastname": "lastname1"
    },
    "id2": {
        "id": "id2",
        "firstaame": "firstname2",
        "lastname": "lastname2"
    }
}

基于阵列的

users: [
    {
        "id": "id",
        "key2": "value2",
        "key3": "value3"
    },
    {
        "id": "id",
        "key2": "value2",
        "key3": "value3"
    }
]

有关对同一问题这个后,我决定(在前端)作为它适合我的要求,并在浏览器更好的性能和更低code。使用JSON对象的符号,而不是对象的数组。

Relating to this post on the same issue, I have decided (on front end) to use the JSON object notation instead of array of objects as it suits my requirements and better performance and less code in the browser.

但问题是,该清单本身不是静态的。我的意思是,正在生成列表,即获取/从DB(NoSQL的)存储,并通过在服务器的Java API的新条目创建。我不能,我应该在后端使用的符号决定(这最终也会影响UI太)。

But the problem is that the list itself is not static. By this I mean the list is being generated i.e. fetched/stored from DB (NoSQL) and created for new entries through a Java API at the server. I am not able to decide on which notation should I use at the back end (which eventually will also affect the UI too).

有关性能,可维护性或可伸缩性的任何想法/建议是AP preciated。

Any thoughts/suggestion about performance, maintainability or scalability is appreciated.

推荐答案

这是一个总的意见基础问题。可能有许多其他的点,但我可以如下指出。

It is a total opinion based question. There might be many other points, but I can point out as below.

基于JSON的方法:
如果我没看错的话,则会使用地图在服务器端实现。

优点:在JavaScript中可以直接使用users.id1,users.id2即无需迭代

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

缺点:在客户端,有些你将如何要求你的JSON即IDS present无论是硬编码,或使用一些动态的做法,会告诉你哪个ID是present在你的JSON。

Disadvantage : On client side, some how you will require the ids present in your JSON i.e. either hard coding it 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.

优点:


  1. 在客户端,您可以直接通过遍历数组,没有
    事先担心它的id是它里面即present没有硬
    编码。

  2. 正如指出的 @JBNizet 后,基于阵列的方法将维持秩序。

  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, array based approach will maintain the order.

缺点:如果您想获取,然后单ID,你需要通过数组进行迭代

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

我们一般不发送客户端上多的信息,所以基于阵列的方法不会产生任何问题。和转化数组地图有可能在两侧(服务器和客户端)如果您想基于ID的方法。

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

这篇关于对象数组VS对象的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆