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

asked Feb 27, 2011 at 16:50

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)
answered Feb 27, 2011 at 16:53
Sign up to request clarification or add additional context in comments.

1 Comment

if first is undefined then why to check next one? If a[1] is undefined then obviously a[1][2] will be undefined. No ?
3

On supported platforms, We can just do:

if(a[3]?.[3]){
 // Do something
}
answered Aug 2, 2020 at 7:48

Comments

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')
answered Feb 27, 2011 at 16:57

1 Comment

In my real code, I have more than a=[['1','2']], however, I get the point. Thank you very much for your answer.
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.

answered Feb 27, 2011 at 16:52

2 Comments

Plus, there should be some new keywords in there. Calling a constructor function without new is a Bad Thing.
Well, that doesn't answer my question, however, thanks for pointing out 'new Array(Array());'. Fixed.
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)
answered Feb 27, 2011 at 16:56

1 Comment

Thanks for your answer. Unfortunately, @Caspar posted earlier (3 minutes earlier), so I must accept his answer. +1, though.
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
}
answered Apr 9, 2022 at 12:48

Comments

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');
}
kupendra
1,0031 gold badge16 silver badges37 bronze badges
answered Feb 27, 2011 at 16:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.