简体   繁体   English

包含键值对javascript的数组

[英]array containing key-value pair javascript

So I have this "story" array: 所以我有这个“故事”数组:

 story[0]=[{"_ref":"/hierarchicalrequirement/15475417305","FormattedID":"US79832","Owner":"A","EstCP":0}]
 story[1]=[{"_ref":"/hierarchicalrequirement/15790056238","FormattedID":"US81776","Owner":"B","EstCP":0}]
 story[2]=[{"_ref":"/hierarchicalrequirement/15790059145","FormattedID":"US81777","Owner":"C","EstCP":7.5}]

How do I get the "FormattedID" key of story[2]? 如何获得故事的“ FormattedID”密钥[2]? I tried: 我试过了:

1. story[2].get("FormattedID")
2. story[2].FormattedID
3. story[2]["FormattedID"]
4. story[2][FormattedID]
5. story[2].getCollection("FormattedID")
6. story[2].get(FormattedID)

None of these works. 这些都不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

story[2] is an array with just one entry. story[2]是一个只有一个条目的数组。 You access that entry via [0] . 您可以通过[0]访问该条目。 That object has the property, so: 该对象具有属性,因此:

story[2][0].FormattedID

...gives you the value. ...为您提供价值。

This may be clearer with some linebreaks. 某些换行符可能会更清楚。 Here's what you're assigning to story[2] : 这是您要分配给story[2]

story[2]= [ // <== Starts array
    {       // <== Starts object
        "_ref": "/hierarchicalrequirement/15790059145",
        "FormattedID": "US81777",
        "Owner": "C",
        "EstCP": 7.5
    }       // <== Ends object
];          // <== Ends array

So story[2][0] gives us the object: 因此, story[2][0]给了我们对象:

{
    "_ref": "/hierarchicalrequirement/15790059145",
    "FormattedID": "US81777",
    "Owner": "C",
    "EstCP": 7.5
}

...which has the FormattedID property. ...具有FormattedID属性。 You can access that using dot notation and a literal property name ( .FormattedID ), or using bracketed notation and a string property name ( ["FormattedID"] ). 您可以使用点表示法和文字属性名称( .FormattedID ),也可以使用括号表示法和字符串属性名称( ["FormattedID"] )访问该属性。

Remove the brackets when you are creating your story objects. 创建故事对象时,请除去括号。 Then you can get to it as you expect. 然后,您可以按预期进行操作。

Example: 例:

 story[0]={"_ref":"/hierarchicalrequirement/15475417305","FormattedID":"US79832","Owner":"A","EstCP":0}

story[0].FormattedID

With the brackets you are actually creating an array with one item at each spot in your main array. 使用括号,您实际上是在创建一个数组,在主数组中的每个位置都有一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM