Find value in JSON array

0

I'm doing a program for a cerezaria that sends empty barrels to fill.

in the stock form you enter the type of beer and a barrel number. Then I get a JSON from the barrels table through an AJAX request. And I want to check if the "active" field of the barrel table of that idbarril is in 1, so if it is empty it is possible to fill it: When loading the stock form I have:

    var accion= "barriles";
    $.ajax({
	  
            type: "POST",
            url: "../gestionweb/views/modules/stock/procesostock.php",
                data:{"accion":accion},
      

            error: function(){
                alert("error petición ajax");
            },
            success:function(data)
            {
                barriles=data;
      
                if (!data.length===0){
                    alert("no hay envases vacios");
                }
                }
                });

 function compruebaBarril(){
            for (var i = 0; i < barriles.length; i++) {
                
                if (barriles[i].idbarril===barril){
        
                                    }
                }}

The above function I want to call by clicking confirm, if it is available the barrel continued the process. But I do not know how to do ... thank you

    
asked by Caruso 06.11.2018 в 13:57
source

1 answer

0

Without seeing the object that you send as a response from Ajax it is difficult to tell you how to look for the asset value. But I can tell you how you could look for it.

The first thing would be to return an object to the ajax response and then parsing it to JSON.

var barriles = JSON.parse(data);

Once you have the result you can print on the console to see what you have and how you have to get it out. For this you use this:

console.log(barrilles);

Now press F12 in the browser and open the console. Here you can see what is the structure of your object and how to get the data.

    
answered by 06.11.2018 в 14:11