1

If we have an array that does not exists and we check the value of the array it gives me an error. "variable is not defined"

for example I have:

var arr = new Array();

arr['house']['rooms'] = 2;

and I use

if ( typeof arr['plane']['room'] != 'undefined' ) )

it says arr['plane'] not defined...

I don't want to use this:

if ( typeof arr['plane'] != 'undefined' ) ) {
    if ( typeof arr['plane']['room'] != 'undefined' ) {

    }
}

In php I use isset that works nice for me, I searched a lot on google to find the answer but I can't...

deceze
  • 510,633
  • 85
  • 743
  • 889
user564285
  • 31
  • 5

3 Answers3

1

The thing to realize is that there are no multi-dimensional arrays in javascript. It is easy to make an array element contain an array, and work with that, but then all references you make have to use that consideration.

So, you can do

arr = [];  // or more appropriately {}, but I'll get to that soon
arr['house'] = [];
arr['house']['rooms'] = 2;

But doing

arr['house']['rooms'] = 2;

should give you an error unless you've already defined arr and arr['house'].

If you've defined arr but not arr['house'], it's valid syntax to reference arr['house'] - but the return value will (appropriately) be undefined.

And this is where you're at when you're looking at arr['plane']['room']. arr is defined, so that's ok, but arr['plane'] returns undefined, and referencing undefined.['room'] throws an error.

If you want to avoid the errors and have multiple levels of reference, you're going to have to make sure that all the levels but the lowest exist.

You're stuck with if (arr && arr['plane'] && arr['plane']['room']).

Or perhaps if (arr && arr['plane'] && room in arr['plane'] would be more accurate, depending on your needs. The first will check if arr['plane']['room'] has a truthy value, while the second will check if arr['plane']['room'] exists at all (and could have a falsey value).

Arrays vs objects

Arrays and objects are very similar and can both be accessed with [] notation, so it's slightly confusing, but technically, you're using the object aspect of the array for what you're doing. Remember, all arrays (and everything other than primitives - numbers, strings and booleans) are objects, so arrays can do everything objects can do, plus more. But arrays only work with numeric indices, i.e. arr[1][2]. When you reference an array with a string, you're attempting to access the member of the underlying object that matches that string.

But still in this case, it doesn't matter. There are no multi-dimensional arrays - or objects.

The [] notation with objects is simply a way to check for members of objects using a variable. arr['plane']['rooms'] is actually equivalent to arr.plane.rooms. but perhaps the arr.plane.room notation will help make it more clear why you have to first check arr.plane (and arr).

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
0

Use the following if you want to test for existence in an object:

if ( 'plane' in arr && 'room' in arr.plane ) {
    // Do something
}
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
0

That's not an array, but an object, aka associative array. You can declare it like this:

var aarr = { house: { rooms: 2 } };

Now you can do:

if (aarr.house && aarr.house.rooms) {/* do stuff */ } 

or uglier, but shorter:

if ((aarr.house || {}).rooms) {/* do stuff */ } 

See also...

To more generally traverse an object to find a path in it you could use:

Object.tryPath = function(obj,path) {
    path = path.split(/[.,]/);
    while (path.length && obj) {
      obj = obj[path.shift()];
    }
    return obj || null;
};
Object.tryPath(aarr,'house.rooms'); //=> 2
Object.tryPath(aarr,'house.cellar.cupboard.shelf3'); //=> null

JsFiddle

KooiInc
  • 119,216
  • 31
  • 141
  • 177