iamchriswick
iamchriswick

Reputation: 370

How can I count # of responses in jQuery $.ajax result?

I'm trying to count the number of results in my $.ajax call.

    $.ajax({
        'async': true,
        'crossDomain': true,
        'url': 'XXX',
        'method': 'GET',
        'headers': {
            'key': 'XXX',
            'scope': 'XXX',
            'content-type': 'application/json',
            'cache-control': 'no-cache'
        },
        'processData': false,
        'data': {},
        error: function(jqXHR, textStatus, errorThrown) {
            // Default error
            log('error');

        },
        success: function(data, textStatus, jqXHR) {
            log('success');
            log(data);
            var ArrayContent = data.length;
            log(ArrayContent)

        }
    }).done(function(response) {
        log('done');
    });

My jSon response is Object {3858: Object, 4667: Object, 4668: Object, 4680: Object, 4710: Object}3858: Object4667: Object4668: Object4680: Object4710: Object__proto__: Object

I have tried several solutions found on this site, but I cant get any of them to work.

Any suggestion towards a solution would be very much appreciated.

-C

Upvotes: 2

Views: 21221

Answers (3)

Saad Mujeeb
Saad Mujeeb

Reputation: 106

var json = JSON.parse(result);
var lengthofObject = json.length; // this is the number of elements in your response

Upvotes: -1

Aju John
Aju John

Reputation: 2234

You can find the number of own enumerable properties in the object using this code:

Object.keys(response).length;

Upvotes: 9

Velimir Tchatchevsky
Velimir Tchatchevsky

Reputation: 2815

Check out the length of the json returned to your ajax function

var count = Object.keys(data).length;

Upvotes: 1

Related Questions