Let's suppose I have this dropdown with multiselection:
<select id='grp_option'>
<optgroup label="Group 1">
<option value='Option 1.1'>Option 1.1</option>
</optgroup>
<optgroup label="Group 2">
<option value='Option 2.1'>Option 2.1</option>
<option value='Option 2.2'>Option 2.2</option>
</optgroup>
</select>
I would like to create an array of object like this:
let array = [];
let obj, obj2;
obj[label] = value;
obj2[label2] = value2;
array.push(obj);
array.push(obj2);
Where label is the optgroup label of each selected option and value is the value of each selected option.
Do you have any idea? Thank you
To do what you require you can use the
map()method. Note that you can provide a variable in bracket notation as the name of the property.Also note that I added the
multipleattribute to theselectto allow more than a singleoptionto be selected - building an array of the values makes no sense without this.