MyDaftQuestions
MyDaftQuestions

Reputation: 4691

Why is this object not an array

I'm not a Javascript person, but have inherited the joys. So far so good.

My javascript object, in firebug, looks like this

enter image description here

This shows that the array sss has 4 items in the array (0,1,2 and 3).

The complication comes when I expand one of these elements, I see

enter image description here

From my own tests these are not arrays. They are just treated as objects (or so it seems).

Within each DataItem, there is an object, you can just make it out called lzabel. Each DataItem has this value. I need to read the values within a for loop.

So, I would have hoped to use

for (var i = 0; i < sss[0];i++)
{
    var z = sss[0][i]; //This is never executed
}   

but no! No error, but the loop's content is never executed (as if sss[0] has no items).

How do I loop through like this?

for (var i = 0; i < sss[0];i++)
{
    var z = sss[0][i]["lzabel"];
}   

EDIT

I added the following code

var t1 = sss.length;
var t2 = sss[0].length;

Firebug reports t1 = 4, and t2 as undefined.

Upvotes: 0

Views: 93

Answers (3)

codeKonami
codeKonami

Reputation: 960

Something like this?

for(i=0;i<sss.length;i++){
 for(j=1;j<parseInt(Object.keys(sss[i]).length)+1;j++){
     console.log(sss[i]["DataItem"+j].Izabel);
  }
 }

Upvotes: 0

Nate
Nate

Reputation: 322

To get Izabel ...

var sssData = sss[0];   // FYI: sss[0].length won't work because it's an object not an array

for (x in sssData) {
    var dataItem = sssData[x];
    console.log( dataItem.Izabel );    // one way to get Izabel property value
    console.log( dataItem["Izabel"] )  // two ways to get Izabel property value
}

Upvotes: 1

neelsg
neelsg

Reputation: 4842

You should be able to loop like this:

for (i in sss[0]) {
    var z = sss[0][i];
}

Upvotes: 0

Related Questions