Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Json count elements in callback array

I have a simple Ajax Call and want to count the elements in the data array that comes back. But .length() doesn't work somehow. Do you know a better way? Thank you!

My Code so far:

$.ajax({
                async:   true,
                type:        'post',
                cache:       false,
                url:         'location.php',
                data:        {myJson: dataArray},
                dataType   : 'json',
                success: function(data){
                    var ArrayContent = data.length();
                    if(ArrayContent == 2) {
                        $('#Result').removeClass('loading').empty();
                        $('#PlzInput').val(data[0]);
                        $('#Result').html('Ihr Standort: '+data[1]);
                    } else if( ArrayContent == 1 ) {
                        $('#Result').removeClass('loading').empty();
                        $('#Result').html(data[0]);
                    }
                }
        });
like image 957
Sebsemillia Avatar asked Dec 03 '25 02:12

Sebsemillia


1 Answers

Remove the braces. i.e:

var ArrayContent = data.length;
like image 84
Mattyod Avatar answered Dec 04 '25 15:12

Mattyod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!