tags:

views:

96

answers:

2

HI,

I am looking at this bit of code:

var length = 0;
for (recordId in publicationTableIndexes[sortColumnNumber]){
 length++;
}

And I am wondering if there is a way of getting the same result without the loop?

publicationTableIndexes is an array containing 5 arrays. If I try publicationTAbleIndexes[sortColumnNumber].length I get undefined.

Ideas?

Thanks, Ron.

Ok, as per the suggestions I did some testing and realised that the sub element is actually an object. The construcotor code:

function sortTableByCreatingIndex(table, sortingColumnNumber, sortOrder, superTable){
var length = 0;

//Length -1 due to the array doing an upwards comparison, if length not adjusted null object error.
length = table.length - 1;

for (recordId = 0; recordId <= length; recordId++){
    this[recordId] = recordId;
}

I actually haven't encountered the 'this' usage before and was quite intrigued by it. What I am interested in knowing how do I take it out and define it as an array. How does 'this' work because looking at the code its not obvious where it is getting its values from, a bit kind of 'like magic' it knows what object to reference.

Thanks, R.

A: 

Sounds like the data in the array is not what your expecting it to be. What i would do is get firebug, and put in the following just before your loop

console.debug(publicationTableIndexes);

That will show you the entire contents of the array, and you can see what is exactly in it.

Zoidberg
?I have no problem with the data it holds, I just want to know the length of the array!
flavour404
flavour, nobody here knows that it is an array -- it doesn't look like an array (it's a member of an array -- who knows what it is) and it doesn't talk like an array (.length is undefined, f'r instance). you are confident in the data, but we haven't seen it. either try some of the methods suggested (and report on their failure in detail), or show us the data, as well.
Michael Paulukonis
I am trying the methods described.
flavour404
I am simply suggesting you ensure you have the data you think you have. Just like OtherMichael said, if length is null, good chances you don't actually have an array there.
Zoidberg
+3  A: 

The data stored in each entry in your publicationTableIndexes is probably being stored using an object which doesn't have a .length property.

SO5223 covers the 'best' way to determine the length of an object.

gnarf
Makse sense.
Zoidberg