Answer the question
In order to leave comments, you need to log in
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
if (Array.isArray(element) && !element.length) {
// пустой массив
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question