在JavaScript中访问数组中对象的值[重复]

时间:2022-01-05 17:40:29

This question already has an answer here:

这个问题在这里已有答案:

I'm aware that properties in objects can be retrieved either .notation or wrapping key as a string expression in a [] suffix. for example

我知道对象中的属性可以作为[]后缀中的字符串表达式检索.notation或wrap key。例如

var character = {
   "name" : "Gloria",
   "feature" : "Dance"
};

console.log("using .notation: "+character.name);
console.log("using []suffix: "+character["name"]);

And it's works pretty well. But, When I do the same by retrieving values from an array of objects, .Notation is simply not working. The codes shows below.

它的效果非常好。但是,当我通过从对象数组中检索值来执行相同操作时,.Notation根本不起作用。代码如下所示。

var info = {
"full_name" : "Some Name",
"title" : "Some title",
"links" : [
        { "blog"     : "http://iviewsource.com" },
        { "facebook" : "http://facebook.com/iviewsource" },
        { "youtube"  : "http://www.youtube.com/planetoftheweb" },
        { "podcast"  : "http://feeds.feedburner.com/authoredcontent" },
        { "twitter"  : "http://twitter.com/planetoftheweb" }
    ]
};

then, when I try to retrieve values from each object in an array with the following snippet

然后,当我尝试使用以下代码段从数组中的每个对象检索值时

for(var i = 0; i < info.links.length; i++) {
    for(var key in info.links[i]) {
        console.log("key is: "+key+" and it's value: "+info.links[i][key]);         
    }
}

In the above code, to retrieve the values I'm using info.links[i][key] and it's works as expected but if I use info.links[i].key, it just gives undefined which I'm not expected.
I wonder why? It confuses me a lot.

在上面的代码中,要检索我正在使用info.links [i] [key]的值,并且它按预期工作但如果我使用info.links [i] .key,它只是给出undefined,我不是预料到的。我想知道为什么?这让我很困惑。

2 个解决方案

#1


0  

key is not a property of that object, that is why it doesn't work. Key is another element with that key that you want to access.

key不是该对象的属性,这就是它不起作用的原因。 Key是您要访问的另一个具有该键的元素。

In JavaScript,

obj.key

Would access the key property of obj object. However the following code,

将访问obj对象的key属性。但是下面的代码,

obj["key"]

Would access the element at key "key". You should consider paying more attention to mapped values, also known as, Dictionary or Map.

将在键“key”访问元素。您应该考虑更多地关注映射值,也称为字典或地图。

#2


0  

If you literally use info.links[i].key it won't work since there is no value associated with the key named key. If you meant something else, please edit the question to reflect that.

如果你真的使用info.links [i] .key它将无法工作,因为没有与名为key的键相关联的值。如果您有其他意思,请编辑问题以反映这一点。

It is also a bit misleading to have a data structure where the name of the link is the key. Would be better to have keys name and link, then you could get them in the same way for every link, with info.links[i].name and info.links[i].link

拥有一个链接名称是密钥的数据结构也有点误导。最好有密钥名称和链接,然后你可以用相同的方式为每个链接获取它们,使用info.links [i] .name和info.links [i] .link

#1


0  

key is not a property of that object, that is why it doesn't work. Key is another element with that key that you want to access.

key不是该对象的属性,这就是它不起作用的原因。 Key是您要访问的另一个具有该键的元素。

In JavaScript,

obj.key

Would access the key property of obj object. However the following code,

将访问obj对象的key属性。但是下面的代码,

obj["key"]

Would access the element at key "key". You should consider paying more attention to mapped values, also known as, Dictionary or Map.

将在键“key”访问元素。您应该考虑更多地关注映射值,也称为字典或地图。

#2


0  

If you literally use info.links[i].key it won't work since there is no value associated with the key named key. If you meant something else, please edit the question to reflect that.

如果你真的使用info.links [i] .key它将无法工作,因为没有与名为key的键相关联的值。如果您有其他意思,请编辑问题以反映这一点。

It is also a bit misleading to have a data structure where the name of the link is the key. Would be better to have keys name and link, then you could get them in the same way for every link, with info.links[i].name and info.links[i].link

拥有一个链接名称是密钥的数据结构也有点误导。最好有密钥名称和链接,然后你可以用相同的方式为每个链接获取它们,使用info.links [i] .name和info.links [i] .link