0

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.

flavour404
  • 6,184
  • 30
  • 105
  • 136
  • 4
    I think you rather have an object (`{}` literal) than an array (`[]` literal). Arrays do have the `length` property, objects do not. – Gumbo Sep 04 '09 at 18:42
  • no, its an array publicationTableIndexes = new Array(); – flavour404 Sep 04 '09 at 18:55
  • 2
    the publicationTableIndexes is an array, sure, but what about publicationTableIndexs[0], is that an array or an object literal? – gnarf Sep 04 '09 at 19:05
  • And what type is `publicationTAbleIndexes[sortColumnNumber]`? Try `publicationTAbleIndexes[sortColumnNumber].constructor`. – Gumbo Sep 04 '09 at 19:16
  • Gumbo you were right, the sub element is an object. The constructor code is above. – flavour404 Sep 04 '09 at 19:32

2 Answers2

3

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.

Community
  • 1
  • 1
gnarf
  • 105,192
  • 25
  • 127
  • 161
0

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
  • 10,137
  • 2
  • 31
  • 53
  • ? I have no problem with the data it holds, I just want to know the length of the array! – flavour404 Sep 04 '09 at 18:56
  • 1
    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 Sep 04 '09 at 19:12
  • I am trying the methods described. – flavour404 Sep 04 '09 at 19:31
  • 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 Sep 04 '09 at 19:37