使用自定义键创建对象数组 [英] Create an array of Objects with custom keys

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

问题描述

目前,我有一个对象数组,如下所示:

Currently, I have an array of objects, that looks like this:

var arr = [{
            id: UNIQUE_ID,
            title: 'TITLE'
          }, {
            id: UNIQUE_ID,
            title: 'TITLE'  
          }];

这里困扰我的是,在特殊情况下,我必须遍历数组并显示数据用于匹配ID。我想只为特定对象获取所有内容,其中包含我想要的ID,就是这样。如果数组看起来更像这样对我来说会容易得多:

What is bothering me here, is that in particular cases I have to loop through the array and display data for matching ID. I want to just fetch everything for that particular object, thas has the ID I want, and that's it. It would be a lot easier for me if the array looked more like this:

var arr = [{
            id: {
              title: 'TITLE'
            },
            id: {
              title: 'TITLE'
            }
          }]

ID来自生成随机数的方法的结果,因此我需要输入变量或方法请求此ID。

The ID comes from a result of a method that generates a random number, so I'll need to put a variable or a method call for this id.

我不确定这是否可行,因为我没有找到这方面的具体例子,但我也希望听到其他解决方案。

I'm not sure this is possible, since I found no particular example on this, but I'd like to hear another solutions as well.

推荐答案

你可以通过完全删除数组,只使用一个对象来做到这一点:

You can do that, by removing the array entirely, just using an object:

var items = {};
items["some id"] = {title: 'first title'};
items["another id"] = {title: 'second title'};

或者如果你在变量中有密钥:

Or if you have the key in a variable:

var key = "a third id";
items[key] = {title: 'third title'};

稍后,如果您想根据密钥查找其中一个条目:

Later, if you wanted to look up one of those entries based on a key:

var key = "another id";

你这样做:

console.log(items[key].title); // "second title" (if key is "another id")

如果你需要知道什么对象中的所有键都是,你可以使用 Object.keys

If you need to know what all of the keys in the object are, you can use Object.keys:

var arrayOfKeys = Object.keys(obj);

(注意 Object.keys 是一个ES5功能;在较旧的浏览器上,你需要为它添加一个垫片,这是微不足道的。)

(Note that Object.keys is an ES5 feature; on older browsers, you need to add a shim for it, which is trivial.)

注意我在上面第一个代码块中填充对象的方式。如果您可以对密钥进行硬编码,则可以使用对象初始化程序执行此操作:

Note the way I populated the object in the first code block above. If you can hardcode the keys, you can do that with an object initializer:

var items = {
    "some id":    {title: 'first title'},
    "another id": {title: 'second title'}
};

...但是如果你想使用一个变量来做这件事就不能这样做键名,因为你不能把变量放在的左侧:(它看起来像一个文字键名)。也就是说,如果你有

...but you can't do it that way if you want to use a variable for the key name, because you can't put the variable on the left-hand side of the : (it'll look like a literal key name). That is, if you have

var key = "a third id";

然后这不起作用:

var items {
    key: {title: "third title"} // <==== Doesn't do what we want
};

密钥是key,不是第三个id(就像我们一直在使用的 title 键)。这就是为什么在上面的第一个块中,我创建了对象,然后再单独设置属性。

The key would be "key", not "a third id" (just like the title key we've been using). That's why in the first block above, I created the object and then set the properties separately afterward.

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

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