Retrieve value of an array of json array with jquery

1

I have the following data that is returned in json to recover the ones with jquery

$response = array($query_license,$query_estate);
    if ($this->request->is('ajax')) {
        echo json_encode($response,TRUE);
        die();
    }

this is the answer of the json in php code

[[{"cantidad_pendiente":5}],[{"estado":3}]]

my function to recover the json data

$.ajax({
            type: 'GET',
            async: true,
            cache: false,
            url: license,
            dataType: 'json',
            data:{id_user_license:id_user_license,id_evaluation:id_evaluation},
            success: function (data){
                $.each(data,function(i,s){
                    console.log(s);
                    if(s[0].cantidad_pendiente === 0){
                        $("#message_license").html('<p>Usted no cuenta con licensias disponibles</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>');
                    }else if(s[0].estado === 3){
                        $("#message_license").html('<p>Usted cuenta con '+ s[0].cantidad_pendiente +'</p>');
                        $("#message_consumed").html('<p>Esta evaluacion ya consumio licencia</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>');
                    }else{
                        $("#message_license").html('<p>Usted cuenta con '+ s[0].cantidad_pendiente +'</p>');
                        $("#modal_report__footer").html('<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button> <button type="submit" class="btn btn-danger">Consumir</button>');
                    }
                });
            }
        });

now verify the data with console.log(data); this was the result

Now when I do the same but within each

$.each(data,function(i,s){
          console.log(s)
 }

I find this

Now my question is how I position myself in quantity_and show its value to the user, because doing so $("#message_license").html('<p>Usted cuenta con '+ s[0].cantidad_pendiente +'</p>'); within my if it shows me undefined maybe I'm doing wrong: (

    
asked by Jonathan Cunza 20.01.2017 в 18:07
source

1 answer

1

Your problem is that your JSON array is badly formed but I provide you with a patch so you can work without altering your JSON.

data = [[{"cantidad_pendiente":5}],[{"estado":3}]];
$.each(data,function(i,s){
    if (data[i][Object.keys(data[i])].cantidad_pendiente){
        console.log("Cantidad pendiente: "+data[i][Object.keys(data[i])].cantidad_pendiente);
    }
    if (data[i][Object.keys(data[i])].estado){
        console.log("Estado: "+data[i][Object.keys(data[i])].estado);
    }
});

With these general validations you should be able to identify if you are processing STATE or PENDING AMOUNT to execute the routine you need.

I do not know how you are forming this array but it is not possible to access the index through the key that interprets the console, that is why I use the Prototype Object.keys () to recover the KEY of the last level of each array. It should be noted that this solution is only valid because the last level of your array only has one KEY.

I'll give you an example as a proof of concept: link

    
answered by 20.01.2017 / 21:55
source