Get values from a json / object

Asked

Viewed 753 times

0

I have the following json:

inserir a descrição da imagem aqui

And 2 selects, one for state and one for city, I have already filled the status correctly using this json:

        //combobox estados
    $.getJSON('http://api.doupenglish.com.br/unidades', function (unidades) {
        var selectestado = $("#selectestado");
        var selectcidade = $("#selectcidade");

        var estados = unidades.Estados;
        //não consigo recuperar as cidades do json aqui
        var cidades = unidades.Estados.values;

        // console.log(cidades)
        var optionsestados = '<option hidden >Selecione um estado</option>';
        $.each(estados, function (estado) {
            optionsestados += '<option value="' + estado + '">' + estado + '</option>';
        });
        selectestado.html(optionsestados);
        selectestado.change(function () {
            var estadoselecionado;
            selectestado.find("option:selected").each(function () {
                estadoselecionado = $(this).text();
                console.log(estadoselecionado)
            });

        })

    });

I need that when selecting a state it lists all the cities of that state, the problem is that I am not able to get the "list" of cities in this json, tried several things but without success. I don’t know if it’s the json that is badly "formatted" but since I don’t have a key to the cities, I can’t access.


EDIT I was able to get an object from the cities:

 $.each(estados, function (estado, cidades) {
                    if (estado === estadoselecionado){
                        console.log(cidades)
                    }
                });

inserir a descrição da imagem aqui

However I still can’t get the values of this object as name, street etc

  • tried data[0].bairro ?

  • @adventistaam yes, from Uncaught error Typeerror: Cannot read Property 'neighborhood' of Undefined

  • if you put in return javascript console.log( data ) what returns?

  • I don’t use the variable with name date, but I think you are referring to the json received with name of units, then return me this: http://prntscr.com/ioq50o

  • The same full json

  • Actually date means given in English, try cidades[0].bairro what happens?

  • Exactly like my first comment ( I had done with cities)

Show 3 more comments

1 answer

1


To get street values, number, email, etc, you have to make a for each inside the other, for example:

selectestado.change(function() {
    let estadoselecionado = $(this).val();

    $.map(estados[estadoselecionado], function (cidades) {
        $.map(cidades, function (enderecos) {
            console.log( `Rua: ${enderecos.rua}, ${enderecos.numero}` )
        });
    });
})

But if you just want to get the name of the cities, just use:

selectestado.change(function() {
    let estadoselecionado = $(this).val();

    $.map(estados[estadoselecionado], function (cidades, cidade) {
        console.log( cidade )
    });
})
  • worked, map and each are the same thing?

  • 1

    @Igoroliveira No, these methods are different. The each is used for immutable objects, whereas the map is most used when we want to return an array with the new values (yes, the map also serves to filter values). But both run arrays, objetos etc. I updated the example to make it more suitable.

  • Just one thing, with each not working, only with map as it was before

Browser other questions tagged

You are not signed in. Login or sign up in order to post.