I came across to this problem. I have defined an array and populated it with objects,
If first case my data array look like this on firebug console:
data = [
Object { code="6", id="1073148", key="82628835"},
Object { code="17", id="2073140", key="83628837"}
]
And when I check its length I get 2 from data.length
In second case I have
data = [
[Object { code="6", id="1073148", key="82628835"}],
[Object { code="17", id="2073140", key="83628837"}]
]
And this one returns 2 also when I check data.length
Can someone explain me why I am getting same lengths for this different cases.
Edit after some answers
data is populated using $.parseJSON what is confusing to me is
in fist case I can access value of code by data[0].code and in second case
I cannot access it by data[0][0].code
If they are the same children of array why I cannot access them using same indices.
Then why they have same length?
Because data.length is only 2 in both cases
Further explained:
Each object contains multiple values, however, you’re not calling for the length of those objects.
data.length = get me the count of children in data (not grandchildren and cousins and neices and nephews) JUST CHILDREN.
In both of those cases, regaurdless of what the objects are, there is 2 of them in data, thus the length = 2
To furter:
It doesn’t matter what those objects are, heck you could have one string and one child array, that would still return 2 because it would be 2 objects inside the object we’re asking for length on
The reason for access is simple, in the first instance, your children are typical Objects, as seen in your example:
Thus you can access obect 1 as data[0].
HOWEVER in your second example, the children are ARRAY Type Objects, thus, each child could have multiple values. For Example:
As you can see, your second example does NOT have two children OBJECTS, instead it contains 2 Children ARRAYS that each contain one child object. Thus the need to get object 1 becomes
data[0][0]Where data[0] is getting the first array child and the second [0] is getting that arrays child object
Get it?
If not just let me know!