Reputation: 1249
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?
Upvotes: 0
Views: 193
Reputation: 22580
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
UPDATE to answer second question brought up
The reason for access is simple, in the first instance, your children are typical Objects, as seen in your example:
data = [
// These two CHILDREN are Typical Objects
Object { code="6", id="1073148", key="82628835"},
Object { code="17", id="2073140", key="83628837"}
]
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:
data = [
[Object { code="6", id="1073148", key="82628835"}],
[Object { code="17", id="2073140", key="83628837"}]
]
// Your example here has 2 children but each child is an ARRAY type Object,
// Each array has one child which is the object in each one
// thus you can call data[0][0] & data[1][0]
// Could also be
data = [
[Object { code="6", id="1073148", key="82628835"},
Object { code="17", id="2073140", key="83628837"}]
]
// in this later case, there is only one child
// this one child, like in your example, is an ARRAY, but in this later example,
// it has TWO children of its own
// thus you can call data[0][0] and data[0][1]
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!
Upvotes: 4
Reputation: 79850
It is really easy when you separate it, See below.
Case 1: Array containing 2 objects.
data => [
Object { code="6", id="1073148", key="82628835"},
Object { code="17", id="2073140", key="83628837"}
]
Case 2: Array containing 2 arrays with each containing an object.
data => [
[Object { code="6", id="1073148", key="82628835"}],
[Object { code="17", id="2073140", key="83628837"}]
]
And so the length is 2 as data.length
contains 2 items in both cases. .
Upvotes: 3