64

I have a set of data that is being passed on by the PHP's json_encode function. I'm using the jQuery getJSON function to decode it:

$.getJSON("url", function (data) {
    console.log(data);
});

The output looks like this in the console:

Object {1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object, 8: Object, 9: Object, 10: Object}

I can access each array by using data[1], data[2] etc, but to make it easier I thought of looping thought it so I can access all at once:

$.getJSON("url", function (data) {
    for (var i = 0, len = data.length; i < len; i++) {
        //do something
    }
});

However I can't get this to work because the data.length returns the value undefined. What is wrong and how can I fix it?

7
  • That isn't an array. It is an object. You can't loop through objects like you can arrays. Commented Feb 4, 2014 at 12:21
  • It's object not an array Commented Feb 4, 2014 at 12:21
  • 24
    Try Object.keys(data).length Commented Feb 4, 2014 at 12:22
  • 2
    Looks as though it's not an array but an arbitrary object. If you have control over the PHP serialization, you might be able to change that. Commented Feb 4, 2014 at 12:22
  • 1
    +1 to what Scott said: it's quite easy to turn this into a real array, replacing your json_encode($something) with json_encode(array_values($something)). Commented Feb 4, 2014 at 12:25

5 Answers 5

91

Objects don't have a .length property.

A simple solution if you know you don't have to worry about hasOwnProperty checks, would be to do this:

Object.keys(data).length;

If you have to support IE 8 or lower, you'll have to use a loop, instead:

var length= 0;
for(var key in data) {
    if(data.hasOwnProperty(key)){
        length++;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

32

One option is:

Object.keys(myObject).length

Sadly it not works under older IE versions (under 9).

If you need that compatibility, use the painful version:

var key, count = 0;
for(key in myObject) {
  if(myObject.hasOwnProperty(key)) {
    count++;
  }
}

1 Comment

saved me a great time after all those years, thanks!
7

It looks as though it's not an array but an arbitrary object. If you have control over the PHP serialization, you might be able to change that.

As raina77ow pointed out, one way to do this in PHP would be by replacing something like this:

json_encode($something) 

with something like:

json_encode(array_values($something))

But don't ignore the other answers here about Object.keys. They should also accomplish what you want if you don't have the ability or the desire to change the serialization of your object.

Comments

2

try this

Object.keys(data).length

If IE < 9, you can loop through the object yourself with a for loop

var len = 0;
var i;

for (i in data) {
    if (data.hasOwnProperty(i)) {
        len++;
    }
}

Comments

1

An easy fix to this question is to add '[' in the start of your json file, and ending it with a ']'. This solved it for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.