简体   繁体   中英

How to populate drop down using two dimension array in javascript


I'm trying to create two dimensional array and populate a dropdown using it.
But I'm getting Uncaught SyntaxError: Unexpected string error.

var cuisines = ["001","Australian"],["002","American"];     

var sel = document.getElementById('CuisineList');
for(var i = 0; i < cuisines.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = cuisines[i][1];
    opt.value = cuisines[i][0];
    sel.appendChild(opt);
}
<select id="CuisineList"></select>


Please help me. Thanks.

You need outer brackets when defining your two dimensional array.

var cuisines = [["001","Australian"],["002","American"]];

Also, looks like you're using the letter "o" instead of the number 0 for one of your indexes.

I did not investigate your code deeply but you have error:

 opt.value = cuisines[i][o];

should be

 opt.value = cuisines[i][0];

you are using "o" letter instead of 0 (zero)

Change var cuisines = ["001","Australian"],["002","American"]; to var cuisines =[ ["001","Australian"],["002","American"]]; so you actually have a two dimensional array, and change your [o] to [0] .

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