Good afternoon friend, you have a problem with this JSON, it does not have the best structure for you to work, the ideal is that the municipality was unique something like:
[
{
"municipio":"pesqueira",
"estabelecimentos": {
{"name": "UBS pesqueira","cod":"7866"},
{"name": "UBS pesqueira","cod":"7866"}
}
}
]
But if you can’t touch this JSON structure you can filter the municipalities of this JSON to then mount the settlement values
const municipios = [...new Set(objeto.map(obj => obj.municipio)]
const selectMunicipios = document.querySelector('#município_id')
selectMunicipios.onchange = () => {
const municipio = selectMunicipios.value
const filtrado = objeto.filter(item => item.municipio === municipio)
const estabelecimentos = filtrado.map(item => item.estabelecimento)
const selectEstabelecimento = document.querySelector('#estabelecimento_id')
selectEstabelecimento.innerHtml = ''
estabelecimentos.forEach(estabelecimento => {
const option = document.createElement('option')
option.innerText = estabelecimento
selectEstabelecimento.appendChild(option)
})
}
municipios.forEach(municipio => {
const option = document.createElement('option')
selectMunicipios.appendChild(option)
})
This code should work if I didn’t write anything wrong ;), where I put the name object means that you must call your JSON object.
Remembering this code is only necessary if you really can’t fix your JSON structure, if you fix this JSON would be much better and easier to work with.
I hope I’ve helped!
You need to turn this json object. And then add option in select and then create event when a select is changing
– Maury Developer
Refers to a COMBO BOX ? You select the city, and load the establishments into another <select>?
– Risk
Misspelled question
– user60252
@Leocaracciolo Chegai leo. rs
– Risk
Yes Risk, selects the city and loads the establishments, both by name!
– sedarky
Any more uploads or just this @sedarky
– Risk
Just that same @Risk
– sedarky