How to create two select box from the given object? I need to create two select option box with Country dropdown and State dropdown Selection.
const optionsList = ko.observableArray ([
{
'AD':{'name':'Andora'},
'AE':{'name':"United Arab Emirates"},
'AG':{'name':"Antigua & Barbuda"},
'ES':{'name':"Spain", 'regions':{
"22": {
"code": "A Coruсa",
"name": "A Coruña"
},
"23": {
"code": "Alava",
"name": "Alava"
},
"24": {
"code": "Albacete",
"name": "Albacete"
}
"25": {
"code": "Zaragoza",
"name": "Zaragoza"
}
}},
'AT':{'name':"Austria", 'regions':{
"95": {
"code": "WI",
"name": "Wien"
},
"96": {
"code": "NO",
"name": "Niederösterreich"
}
}},
'AU':{'name':"Australia", 'regions':{
"569": {
"code": "ACT",
"name": "Australian Capital Territory"
},
"570": {
"code": "NSW",
"name": "New South Wales"
},
"571": {
"code": "VIC",
"name": "Victoria"
},
"572": {
"code": "QLD",
"name": "Queensland"
}
}}
}
])
I want to create two dropdown using knockout JS.
- Country List
- Region list if object contains the region list in object. (like Spain, Austria and Australia)
I am not sure how to create two select box in knockout using the above given object.
Thank you for any useful information.
You can define a
regionscomputed that looks at a selected country and only returns its regions:The data you posted uses objects where I'd expect arrays, so it's slightly different. Here's a runnable example: