简体   繁体   English

用Javascript访问词典列表

[英]Access list of Dictionary in Javascript

I have a list of dictionary items and I want to access them in Javascript. 我有一个词典项目列表,我想用Javascript访问它们。 How can I do this? 我怎样才能做到这一点? It is an ajax result and result.d gives the list. 这是一个ajax结果,result.d给出列表。 (ListItem1, ListItem2, ListItem3) If I use result.ListItem1[0] I get the value. (ListItem1,ListItem2,ListItem3)如果我使用result.ListItem1 [0],则会得到该值。 How can I access the key? 如何访问密钥?

result.ListItem1[0].key or result.ListItem1[0]['key'] result.ListItem1[0].keyresult.ListItem1[0]['key']

Replace "key" with whatever key you are actually wanting to access. 将“ key”替换为您实际想要访问的任何密钥。

You could enumerate all the properties of your object 您可以枚举对象的所有属性

 var prop, result = { d: { ListItem1: ['value1'], ListItem2: ['value2'], ListItem3: ['value3'] } }; for (prop in result.d) { if (result.d.hasOwnProperty(prop)) { console.log(result.d[prop]); } } 

In the log you would then see the 3 array values 在日志中,您将看到3个数组值

Thanks for the response. 感谢您的回复。 I used the following to access the Key of the dictionary: 我使用以下内容访问字典的键:

for (t in result.ListItem1) 
{
    type = result.ListItem1[t];
    typeId = t;
    alert(t);
    o.push('<option value="');
    o.push(t);
    o.push('" class="chz-option">');
    o.push(type);
    o.push('</option>');
}
$('#type').html(o.join(''));

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

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