开发者

Check for existence of key in multidimensional array in javascript

开发者 https://www.devze.com 2023-02-13 13:42 出处:网络
Hopefully an easy question. Why is that checking for existence of a key in a multidimensional array: a = new Array(Array());

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 ge开发者_JAVA技巧neral, 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


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)


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')


On supported platforms, We can just do:

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


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.


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)


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
}


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');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消