JSON array zero length even with elements?

I am getting a JSON response from a server and creating a JSON object in javascript. The structure is this:

var response = {
    key1:[],
    key2:[],
    key3:[],
    key4:[],
    key5:[]
}

When the request completes the response object is successfully completed like this:

Object (*expandable):
    key1: Array[0]
    key2: Array[0]
    key3: Array[0]
    key4: Array[20]
    key5: Array[113]

Now later on I want to store the information into a database. I have created a function and I console.log the response object to make sure it’s ok (here it is getting interesting – see comments):

function setupDatabase(){
    console.log(response); // prints the response correctly (see response above)
    console.log(response.key5); //prints key5: Array[0]. If I expand the Array[0] all the elements are inside.
    console.log("key5: "+response.key5.length);//prints 0!!
}

It’s normal for the first 3 keys to be 0 because there are no elements returned for them. The rest 2 are ok. Why do I get this log, while I run 3 console.log commands on the same object in a row? Am I missing something?

Thanks in advance


Source: json

Leave a Reply