9

Hopefully an easy question.

Why is that checking for existence of a key in a multidimensional array:

a = new Array(Array());
a[0][0]='1';
a[0][1]='2';
if(a[1][2] == undefined){
alert("sorry, that key doesn't exist");
} else {alert('good, your key exists');
}

seems not to be working in general, but it works when I check for a first index (in this case, '0') that is 'defined' by a[0][x]. For example, when I ask for a[0][2] (which is not defined), it shows the first alert. However, when I ask for a[1][0], I get:

"Uncaught TypeError: Cannot read property '0' of undefined"

How can I solve this problem?

Thanks

7 Answers 7

12

Check first if the first dimension exists then if the key in the second dimension exists

The logic will return false if the first test returns false, and tests the second dimension only if the first one is true.

  if(a[1] == undefined && a[1][2] == undefined)
1
  • 2
    if first is undefined then why to check next one? If a[1] is undefined then obviously a[1][2] will be undefined. No ?
    – Hafiz
    Commented Mar 12, 2014 at 11:29
3

On supported platforms, We can just do:

if(a[3]?.[3]){
  // Do something
}
2

With the first three assignments your array actually looks like this:

a = [['1','2']]

Reading a[0][2] just returns undefined because a[0] exists but its property '0' is not defined.

But trying to read a[1][0] throws a TypeError because a[1] is already undefined and isn’t an object and thus doesn’t have any properties. This is also what your error message says:

Cannot read property '0' of undefined.

You can solve this problem by first checking for a[1] and then checking for a[1][0] using the typeof operator:

if (typeof a[1] !== 'undefined' && typeof a[1][0] !== 'undefined')
1
  • In my real code, I have more than a=[['1','2']], however, I get the point. Thank you very much for your answer.
    – r_31415
    Commented Feb 27, 2011 at 17:00
1
a = Array(Array())

does not define a multi-dimensional array. It just defines an array with one item, which happens to be another (empty) array. There are no built-in multidimensional arrays in javascript, so you will have to handle things more manually.

2
  • Plus, there should be some new keywords in there. Calling a constructor function without new is a Bad Thing.
    – johusman
    Commented Feb 27, 2011 at 16:58
  • Well, that doesn't answer my question, however, thanks for pointing out 'new Array(Array());'. Fixed.
    – r_31415
    Commented Feb 27, 2011 at 17:04
1

You just need to further qualify the conditional. Since the [1] index of the array is undefined you can't test for values in it.

if(a[1] === undefined || a[1][2] === undefined)
1
  • Thanks for your answer. Unfortunately, @Caspar posted earlier (3 minutes earlier), so I must accept his answer. +1, though.
    – r_31415
    Commented Feb 27, 2011 at 17:07
1

Assuming explicitly arr[i][j] = undefined is not done

Try the below:

(Used last 2nd one today)

if (arr[i]?.[j] === 100) {
    // value at i & j  is 100
}

if (arr[i]?.[j] !== undefined) {
    // value at i & j  exists
}

if (arr[i]?.[j] === undefined) {
    // value at i & j  does not exist
}

if ([100, 200].includes(arr[i]?.[j])) {
    // value at i & j  is 100 or 200
}

if ([undefined, 100].includes(arr[i]?.[j])) {
    // value at i & j does not exist
    //   Or, value exists & it is 100
}

if ([undefined, 100, 200].includes(arr[i]?.[j])) {
    // value at i & j does not exist
    //   Or, value exists & it is 100 or 200
}
0
var a = Array(Array());
a[0][0]='1';
a[0][1]='2';
if(a[1] === undefined || a[1][2] === undefined) {
    alert("sorry, that key doesn't exist");
} else {
    alert('good, your key exists');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.