I am trying to build a select dropdown from the key and values in my json object but I am not able to get the key value pairs together I have been only successful at getting the key names themselves. Below is what I have been able to complete so far.
This is how I would like the select options to look:
<select id="optSelect">
<option value="key1">value1</option> <!-- should come from name1 -->
<option value="key1">value2</option> <!-- should come from name2 -->
</select>
Do I need to make another array to hold the value of each key and then reference accordingly when I build my select options or is there another way or method?
var someObject = {
"Name1": {
"key1": "value1",
},
"Name2": {
"key1": "value2",
}
}
var objArray = [];
for(var i in someObject){
var key = someObject[i];
for(var x in key1){
var nextKey = x;
var nextVal = key1[x];
someObject.push(nextKey);
}
}
var optionSelect = document.getElementById("optSelect"),
selectList = document.createElement("select");
selectList.id = "mySelect";
optionSelect.appendChild(selectList);
for (var i = 0; i < objArray.length; i++) {
var option = document.createElement("option");
option.value = objArray[i]; // need to get key1 in here for each value in someObject
option.text = objArray[i]; // need to get value 1 or 2 etc in here for each value name in someObject
selectList.appendChild(option);
}
for(var x in key1)herekey1is undefinedsomeObject.push(nextKey);should beobjArray.push(nextKey);, to go further:objArray[nextKey] = nextVal;<select>and<option>elements to look? It's unclear from your code what should be set for thevalueand text content<option>element values?