Not sure what to write as a title, but this is my problem:
if(selectedCountry=="India"){
stateArray=[]
Object.keys(jsonObj.India).forEach(function(k) {
stateArray.push(k)
});
}
if(selectedCountry=="USA"){
stateArray=[]
Object.keys(jsonObj.USA).forEach(function(k) {
stateArray.push(k)
});
}
if(selectedCountry=="UK"){
stateArray=[]
Object.keys(jsonObj.UK).forEach(function(k) {
stateArray.push(k)
});
}
if(selectedCountry=="none"){
stateArray=[]
}
for (var i=0; i<stateArray.length; i++){
var stateOption = new Option(stateArray[i],stateArray[i])
if (states.length<stateArray.length+1){
states.appendChild(stateOption);
}
Although the code is working, it is bad way of doing it. Is there a way where I rewrite the code so I don't need the if statements? Because selected country is a string, I cannot use:
Object.keys(jsonObj.selectedCountry).forEach(function(k) {
stateArray.push(k)
});
Is there any way around it? Thx