2015-04-06 49 views
0

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

回答

0

感谢您的答复。我用下面的方式访问字典的键:

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

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

替换为“关键”与你实际上是想获得什么关键。

0

你可以列举你的对象

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数组值