对象与Javascript中的数组对应键/值对 [英] Objects vs arrays in Javascript for key/value pairs

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

问题描述

说你有一个非常简单的数据结构:

Say you have a very simple data structure:

(personId, name)

...并且您想将其中一些存储在JavaScript变量中。我看到它有三个选项:

...and you want to store a number of these in a javascript variable. As I see it you have three options:

// a single object
var people = {
    1 : 'Joe',
    3 : 'Sam',
    8 : 'Eve'
};

// or, an array of objects
var people = [
    { id: 1, name: 'Joe'},
    { id: 3, name: 'Sam'},
    { id: 8, name: 'Eve'}
];

// or, a combination of the two
var people = {
    1 : { id: 1, name: 'Joe'},
    3 : { id: 3, name: 'Sam'},
    8 : { id: 8, name: 'Eve'}
};

第二个或第三个选项显然是如果你有(或者期望你可能有)存储多个值部分(例如,添加他们的年龄或某些东西),所以为了论证,让我们假设在这个结构中永远不会有任何更多的数据值。您选择哪一个,为什么?

The second or third option is obviously the way to go if you have (or expect that you might have) more than one "value" part to store (eg, adding in their age or something), so, for the sake of argument, let's assume that there's never ever going to be any more data values needed in this structure. Which one do you choose and why?

修改:该示例现在显示最常见情况:非连续的ids。

Edit: The example now shows the most common situation: non-sequential ids.

推荐答案

每个解决方案都有其用例。

Each solution has its use cases.

如果您尝试定义一对一的关系(如简单的映射),我认为第一个解决方案是很好的,特别是如果您需要使用该键作为查找键。

I think the first solution is good if you're trying to define a one-to-one relationship (such as a simple mapping), especially if you need to use the key as a lookup key.

第二个解决方案对我来说最为强大,如果我不需要快速查找键,我可能会使用它:

The second solution feels the most robust to me in general, and I'd probably use it if I didn't need a fast lookup key:


  • 这是自我描述,所以你不要
    必须依赖任何人使用
    来知道密钥是用户的ID。

  • 每个对象都是自包含的,
    更好地传递数据
    在别处 - 而不是两个参数
    (id和name)你只是通过
    的人。

  • 这是一个罕见的问题,但有时
    的关键val对于
    用作键可能无效。例如,我曾经
    想映射字符串转换
    (例如,:到>),但是由于:
    不是有效的变量名,我必须
    使用第二种方法。

  • 它可以很容易地扩展,如果你需要
    的某个地方
    向一些(或全部)用户添加更多的数据。
    (对不起,我知道你的为
    参数的缘故,但这是一个
    的重要方面。)

  • It's self-describing, so you don't have to depend on anyone using people to know that the key is the id of the user.
  • Each object comes self-contained, which is better for passing the data elsewhere - instead of two parameters (id and name) you just pass around people.
  • This is a rare problem, but sometimes the key values may not be valid to use as keys. For example, I once wanted to map string conversions (e.g., ":" to ">"), but since ":" isn't a valid variable name I had to use the second method.
  • It's easily extensible, in case somewhere along the line you need to add more data to some (or all) users. (Sorry, I know about your "for argument's sake" but this is an important aspect.)

如果您需要快速查找时间+上面列出的一些优点(传递数据,自我描述),第三个将会很好。但是,如果您不需要快速查找时间,则会更麻烦。另外,无论哪种方式,如果对象中的id与中的id不同,则会冒错误的风险。

The third would be good if you need fast lookup time + some of the advantages listed above (passing the data around, self-describing). However, if you don't need the fast lookup time, it's a lot more cumbersome. Also, either way, you run the risk of error if the id in the object somehow varies from the id in people.

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

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