How to check a json array for emptiness?

  • 0
    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
    				}
    			});
    		}
    	});
    AJAX Noah O'Neill, Nov 23, 2019

  • 1 Answers
  • 0
    if (Array.isArray(element) && !element.length) {
    // пустой массив
    }
    Atticus Guzman

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!