简体   繁体   中英

Access list of Dictionary in Javascript

I have a list of dictionary items and I want to access them in Javascript. How can I do this? It is an ajax result and result.d gives the list. (ListItem1, ListItem2, ListItem3) If I use result.ListItem1[0] I get the value. How can I access the key?

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

Replace "key" with whatever key you are actually wanting to access.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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