I have some JSON data. I cam trying to get similar values from it. Is there any ways to use javascript to do that? I am getting my JSON from the server. But I have a sample. I am trying to load different browser versions into their optgroups
.
Is there a way to do that?
JSON:
[
{
"browser": "Amazon Silk 3.30"
},
{
"browser": "Amazon Silk 49.2"
},
{
"browser": "Edge 42.11"
},
{
"browser": "Edge 42.15"
},
{
"browser": "Google Chrome 0.01"
},
{
"browser": "Google Chrome 0.03"
}
]
HTML
<select id="browsers" style="max-height: 150px; overflow: auto;"></select>
JavaScript
$.getJSON(jsonBrowser, function(data) {
data.forEach((d, i) => {
let browser_val = d.browser.replace(/ /g, "%20");
$('#browsers').append(`<option value=${browser_val} selected>${d.browser}</option>`);
});
});
}
Result: Right now, this code only populates by dropdown.
Amazon Silk 3.30
Amazon Silk 49.2
Edge 42.11
Edge 42.15
Google Chrome 0.01
Google Chrome 0.03
Result I need: I want to use optgroups
in my dropdown to show:
**Amazon**:
Amazon Silk 3.30
Amazon Silk 49.2
**Edge**:
Edge 42.11
Edge 42.15
**Google**:
Google Chrome 0.01
Google Chrome 0.03
Is there a way to group them using JavaScript/jQuery?