Javascript中Dictionary的访问列表

Access list of Dictionary in Javascript

本文关键字:访问 列表 Dictionary Javascript      更新时间:2023-09-26

我有一个字典项列表,我想用Javascript访问它们。我该怎么做?这是一个ajax结果,result.d给出了列表。(ListItem1、ListItem2、ListItem3)如果我使用结果。ListItem1[0]我得到了值。我怎样才能拿到钥匙?

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

将"密钥"替换为您实际想要访问的任何密钥。

您可以枚举对象的所有属性

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]);
  }
}

在日志中,您将看到3个数组值

感谢您的回复。我使用以下内容来访问字典的密钥:

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(''));