javascript 在javascript中检查多维数组中是否存在键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5134468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for existence of key in multidimensional array in javascript
提问by Robert Smith
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:
一般情况下似乎不起作用,但是当我检查由a[0][x]. 例如,当我要求a[0][2](未定义)时,它显示第一个警报。但是,当我要求时a[1][0],我得到:
"Uncaught TypeError: Cannot read property '0' of undefined"
“未捕获的类型错误:无法读取未定义的属性‘0’”
How can I solve this problem?
我怎么解决这个问题?
Thanks
谢谢
回答by Caspar Kleijne
Check first if the first dimension exists then if the key in the second dimension exists
首先检查第一个维度是否存在,然后检查第二个维度中的键是否存在
The logic will return falseif the first test returns false, and tests the second dimension onlyif the first one is true.
false如果第一个测试返回false,则逻辑将返回,并且仅当第一个测试返回时才测试第二个维度true。
if(a[1] == undefined && a[1][2] == undefined)
回答by Gumbo
With the first three assignments your array actually looks like this:
通过前三个分配,您的数组实际上如下所示:
a = [['1','2']]
Reading a[0][2]just returns undefinedbecause a[0]exists but its property '0'is not defined.
读取a[0][2]只是返回,undefined因为a[0]存在但'0'未定义其属性。
But trying to read a[1][0]throws a TypeErrorbecause a[1]is already undefinedand isn't an object and thus doesn't have any properties. This is also what your error message says:
但是尝试读取a[1][0]会抛出一个TypeError因为a[1]它已经undefined不是一个对象,因此没有任何属性。这也是您的错误消息所说的:
Cannot read property '0' of undefined.
无法读取未定义的属性“0”。
You can solve this problem by first checking for a[1]and then checking for a[1][0]using the typeofoperator:
您可以通过首先检查a[1]然后检查a[1][0]使用typeof运算符来解决此问题:
if (typeof a[1] !== 'undefined' && typeof a[1][0] !== 'undefined')
回答by johusman
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.
没有定义多维数组。它只是定义了一个包含一项的数组,而该数组恰好是另一个(空)数组。javascript 中没有内置的多维数组,因此您必须更加手动地处理事情。
回答by Robin W.
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.
您只需要进一步限定条件。由于数组的 [1] 索引未定义,您无法测试其中的值。
if(a[1] === undefined || a[1][2] === undefined)
回答by Manish Trivedi
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');
}

