Javascript 数组值未定义......我该如何测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2673147/
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
Javascript array value is undefined ... how do I test for that
提问by Ankur
I am trying to test to see whether a Javascript variable is undefined.
我正在尝试测试是否未定义 Javascript 变量。
You will see that I am not expecting the value of predQuery[preId] to be 'undefined' if I don't first get an alert saying "its unbelievable". But I often do, so I am guessing that my statement
你会看到,如果我没有首先收到一个警告说“它令人难以置信”,我并不期望 predQuery[preId] 的值是“未定义”。但我经常这样做,所以我猜我的陈述
predQuery[preId]=='undefined')
is not matching the undefined elements properly.
未正确匹配未定义的元素。
if((predQuery.length < preId) || (predQuery[preId]=="") || (predQuery[preId]=='undefined')){
alert("its unbelievable");
alert(predQuery[preId]);
queryPreds[variables] = preId;
queryObjs[variables] = objId;
predQuery[preId] = variables;
}
else {
alert(predQuery[preId]);
var predIndex = predQuery[preId];
queryPreds[predIndex] = preId;
queryObjs[predIndex] = objId;
}
I can add more code if needed.
如果需要,我可以添加更多代码。
回答by deceze
array[index] == 'undefined'compares the valueof the array index to the string "undefined".
You're probably looking for typeof array[index] == 'undefined', which compares the type.
array[index] == 'undefined'将数组索引的值与字符串“undefined”进行比较。
您可能正在寻找typeof array[index] == 'undefined',它比较了type。
回答by CMS
You are checking it the array index contains a string "undefined", you should either use the typeofoperator:
您正在检查数组索引包含一个 string "undefined",您应该使用typeof运算符:
typeof predQuery[preId] == 'undefined'
Or use the undefinedglobal property:
或者使用undefined全局属性:
predQuery[preId] === undefined
The first way is safer, because the undefinedglobal property is writable, and it can be changed to any other value.
第一种方式更安全,因为undefined全局属性是可写的,并且可以更改为任何其他值。
回答by bobince
predQuery[preId]=='undefined'
You're testing against the string'undefined'; you've confused this test with the typeoftest which would return a string. You probably mean to be testing against the special value undefined:
您正在针对字符串进行测试'undefined';您已将此测试与typeof将返回字符串的测试混淆了。您可能打算针对特殊值进行测试undefined:
predQuery[preId]===undefined
Note the strict-equality operator to avoid the generally-unwanted match null==undefined.
请注意严格相等运算符以避免一般不需要的 match null==undefined。
However there are two ways you can get an undefinedvalue: either preIdisn't a member of predQuery, or it isa member but has a value set to the special undefinedvalue. Often, you only want to check whether it's present or not; in that case the inoperator is more appropriate:
但是,您可以通过两种方式获取undefined值:preId不是 的成员predQuery,或者是成员但将值设置为特殊undefined值。通常,您只想检查它是否存在;在这种情况下,in运算符更合适:
!(preId in predQuery)
回答by KooiInc
There are more (many) ways to Rome:
有更多(许多)方式前往罗马:
//=>considering predQuery[preId] is undefined:
predQuery[preId] === undefined; //=> true
undefined === predQuery[preId] //=> true
predQuery[preId] || 'it\'s unbelievable!' //=> it's unbelievable
var isdef = predQuery[preId] ? predQuery[preId] : null //=> isdef = null
cheers!
干杯!
回答by rahul
Check for
检查
if (predQuery[preId] === undefined)
Use the strict equal to operator. See comparison operators
使用严格等于运算符。查看比较运算符
回答by Warty
try: typeof(predQuery[preId])=='undefined'
or more generally: typeof(yourArray[yourIndex])=='undefined'
You're comparing "undefined" to undefined, which returns false =)
尝试:typeof(predQuery[preId])=='undefined'
或更一般地说:typeof(yourArray[yourIndex])=='undefined'
您将“未定义”与未定义进行比较,后者返回 false =)
回答by Bernardo Ravazzoni
This code works very well
这段代码效果很好
function isUndefined(array, index) {
return ((String(array[index]) == "undefined") ? "Yes" : "No");
}

