I have an array that I use to generate a series of options that will be populated into a html select element. This currently works fine. I've done various benchmark testing and .map() is the most efficient approach.
My question is, how can I group the options within an optgroup element. I've tried using nested .map() but it out puts the options ungrouped still.
JS Fiddle https://jsfiddle.net/57gbxrzc/
const myOptsArr = [
{
title: 'Fruit',
values: ['apple','pear','grapes','beer']
},
{
title: 'Animals',
values: ['apple','dog','mouse','cat']
},
// more here
];
let myOpts = `
<option value="">Select Option</option>
${myOptsArr.map(item => `
</optgroup label="${item.title}">${
item.values.map(itemValue => `
<option value="${itemValue}">${itemValue}</option>
`).join('')}
</optgroup>;
`).join('')}
`;
document.getElementById('mySelect').innerHTML = myOpts;
</optgroup label="${item.title}">${instead of opening it. It should be<optgroup label="${item.title}">${.