views:

402

answers:

6

I have a multi-dimensional array, no problem. How do I interrogator one of the arrays in order to ascertain if it actually holds any data? I am working with VS 2008 and what I can see in the debugger is, lets call the element x, is x{...}. However, if I try and use x.length i get the message 'undefined' - so how do I ascertain if the array has nothing in it?

type(x) is returning an object. Here is the constructor code:

function initArray() {
var length = initArray.arguments.length for (var i = 0; i < length; i++)
{ this[i+1] = initArray.arguments[i]; } }

So it should return a .length value, which it isn't!

In the multilevel array, it's the third level down so x = pub[1][8] which should be the target array, but as I said if I then go x.length, or x[].length I get undefined...

Thanks.

A: 

check if the object is an array first:

function isArray(obj) {
  return obj.constructor == Array;
}
Colin
Might want to also check that obj is actually an object first, just to be safe...
Shog9
Checking that it's an object doesn't reveal any more certainty than checking directly to see if it's an array. Many different types of elements identify themselves as objects.
Andrew Noyes
I mean, an object as opposed to undefined.
Shog9
+2  A: 

Are you sure it's an array and not an object? Arrays use [] and objects use {}.

The array's length should be zero upon its definition.

You can check typeof(x) to see if it's undefined.

This is a case where we're shooting in the dark without seeing your code.

Nosredna
Well, this *is* the visual studio debugger he's talking about... But the suggestion is sound.
Shog9
Haha. Good point. I'm going to fire it up and take a look myself.
Nosredna
typeof(x) is returning 'object' so that's cool. Below is the constructor code:
flavour404
A: 

If it's an array created like "x = new Array();" or "x = [];", then it ought to have a .length property. If it doesn't have a .length property, then you are likely dealing with a null reference (no object) or else you aren't dealing with an array.

Chris W. Rea
+1  A: 

It looks like your object x is not an array at all.

If it were, the length would be zero or positive integer.

It sounds like you might have an object and inadvertently added expando properties.

try this:

for(var prop in x) {
    var p = prop; // set a breakpoint on this line and check the value of prop
}
Jeff Meatball Yang
This worked like a top and I was able to work in some other code to do exactly what I wanted... So thanks.
flavour404
No prob! Enumerating an object is a hidden gem in Javascript.
Jeff Meatball Yang
A: 

You could use a boolean operation to check it as you mentioned.

var arrayHasValues = false;
if (x.length) { // <-- Will be 'true' if 'length' is not null or undefined
    if (x.length > 0) {
        arrayHasValues = true;
    }
}

if  (arrayHasValues) {
    // Do Something with Array
}
Chris Pietschmann
A: 

The only vector data type in JavaScript that lacks a length property is Object, so you're likely dealing with an object and not an Array.

Andrew Noyes