【发布时间】:2015-04-06 12:57:18
【问题描述】:
我有一个字典项目列表,我想用 Javascript 访问它们。我怎样才能做到这一点? 这是一个 ajax 结果, result.d 给出了列表。 (列表项 1、列表项 2、列表项 3) 如果我使用 result.ListItem1[0] 我会得到值。如何访问密钥?
【问题讨论】:
标签: javascript ajax list dictionary
我有一个字典项目列表,我想用 Javascript 访问它们。我怎样才能做到这一点? 这是一个 ajax 结果, result.d 给出了列表。 (列表项 1、列表项 2、列表项 3) 如果我使用 result.ListItem1[0] 我会得到值。如何访问密钥?
【问题讨论】:
标签: javascript ajax list dictionary
result.ListItem1[0].key 或 result.ListItem1[0]['key']
将“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(''));
【讨论】: