What would be the best way to turn a json object into a sorted HTML select (straight javascript only)?
The format of the json can change if needed. I have been searching and most documentation says that object keys can not be sorted only arrays can be sorted, but I couldn't figure out how I would modify the structure of the json to make it an array and therefore easily sortable.
var json = {
"group3": [
{"value33": "label33"},
{"value13": "label13"},
{"value23": "label23"}
],
"group1": [
{"value21": "label21"},
{"value31": "label31"},
{"value11": "label11"}
],
"group2": [
{"value22": "label22"},
{"value12": "label12"},
{"value32": "label32"}
]
};
Will become this:
<select multiple="multiple">
<optgroup label="Group 1">
<option value="value11">Label 11</option>
<option value="value21">Label 21</option>
<option value="value31">Label 31</option>
</optgroup>
<optgroup label="Group 2">
<option value="value12">Label 12</option>
<option value="value22">Label 22</option>
<option value="value32">Label 32</option>
</optgroup>
<optgroup label="Group 3">
<option value="value13">Label 13</option>
<option value="value23">Label 23</option>
<option value="value33">Label 33</option>
</optgroup>
</select>
(group?)and use that for the array index. Fill the array. Then, loop through the array using aforloop and write the data out.