P
P
patsanchique2020-11-01 12:39:42
JSON
patsanchique, 2020-11-01 12:39:42

How to check if json array is empty?

I have an Ajax request, the response of which contains an array with one object [{"status":"n"}] , please tell me how to add a check if I get an empty array in the response

$.ajax({ 
    type: 'GET', 
    url: 'status.php', 
    dataType: 'json',
    success: function (data) { 
      $.each(data, function(index, element) {
        if(element['status'] == 0) {
        //do stuff
        }
        if(element['status'] == 1) {
        //do stuff
        }
        if(element['status'] == 2) {
        //do stuff
        }
      });
    }
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-11-01
@patsanchique

if (Array.isArray(element) && !element.length) {
// пустой массив
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question