How to check if an array index exists or not in javascript?(如何在javascript中检查数组索引是否存在?)
问题描述
我使用的是Ti,我的代码如下所示:
var currentData = new Array();
if(currentData[index]!==""||currentData[index]!==null||currentData[index]!=='null')
{
Ti.API.info("is exists " + currentData[index]);
return true;
}
else
{
return false;
}
我正在向currentData
数组传递索引。我仍然无法使用上面的代码检测到不存在的索引。
推荐答案
使用typeof arrayName[index] === 'undefined'
即
if(typeof arrayName[index] === 'undefined') {
// does not exist
}
else {
// does exist
}
这篇关于如何在javascript中检查数组索引是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!