I need to divide my array into groups and options in a <select>. I don't know how to do it dynamically, since my array contains strings divided by :, which means it can have the same groups with options and groups with groups inside it.
My actual problem is how to get the groups and options by the string.
I have an array that's returning this specific data:
const data = [
{ id: 1, raw_name: "vehicle:car:ford"},
{ id: 2, raw_name: "vehicle:motorbike:honda"},
{ id: 3, raw_name: "none"},
{ id: 4, raw_name: "vehicle:car:fiat"},
{ id: 5, raw_name: "vehicle:car:bmw"},
{ id: 6, raw_name: "vehicle:motorbike:suzuki"},
{ id: 7, raw_name: "vehicle:plane:jets:gulfstream"},
{ id: 8, raw_name: "vehicle:bike"}
];
I've tried to split raw_name by : and do a for loop to create the group and the options with the result, but unfortunately it does not organize by itself.
My code:
<select id="bankselect">
</select>
<script>
const select = document.getElementById("bankselect");
let optGroup;
data.forEach((element => {
for (let index = 0; index < element.opt.length; index++) {
if (index + 1 === element.opt.length) {
const opt = document.createElement("option");
opt.value = element.opt[index];
opt.innerText = element.opt[index];
optGroup.append(opt);
} else {
optGroup = document.createElement("optgroup");
optGroup.label = element.opt[index];
}
select.append(optGroup);
}
}))
</script>
My result:
My expected final outcome would be this:
<select id="bankselect">
<option>none</option>
<optgroup label="vehicle">
<option>bike</option>
<optgroup label="car">
<option>ford</option>
<option>fiat</option>
<option>bmw</option>
</optgroup>
<optgroup label="motorbike">
<option>honda</option>
<option>suzuki</option>
</optgroup>
<optgroup label="plane">
<optgroup label="jets">
<option>gulfstream</option>
</optgroup>
</optgroup>
</optgroup>
</select>


First, turn that flat data into nested data, and then use the nested data to recursively build your
<select>content:This ends up generating the following (bad!) nesting:
Because bear in mind: you can't nest
<optgroup>elements. That is to say, you "can", but it's invalid HTML and while browsers will do something reasonably sensible with that invalid HTML, it's still invalid HTML and you should reconsider how to show this data, because you're basically using the wrong UI element for this data.