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

查看:35
本文介绍了对象数组 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"
    }
]

关于这篇关于同一问题的帖子,我决定(在前端)使用 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).

对性能、可维护性或可扩展性的任何想法/建议表示赞赏.

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 的方法:如果我没有错,那么这将在服务器端使用 Map 实现.

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

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

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

缺点:在客户端,您将如何要求 JSON 中存在 id,即对其进行硬编码或使用一些动态方法来告诉您 JSON 中存在哪个 id.

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/List 实现.

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

优势:

  1. 在客户端,您可以直接遍历数组,无需提前担心其中存在哪个 ID,即不难编码.
  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天全站免登陆