我正在使用Titanium,我的代码看起来是这样的:

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数组。使用上面的代码,我仍然无法检测到不存在的索引。


当前回答

if(typeof arrayName[index] == undefined) {
    console.log("Doesn't exist")
}
else {
console.log("does exist")
}

其他回答

var fruits =["香蕉","橙子","苹果","芒果"]; if(fruit . indexof ("Banana") == -1){ Console.log('项目不存在') }其他{ console.log(“项目存在”) }

我必须把techfoobar的答案包装在一个try. catch块中,就像这样:

try {
  if(typeof arrayName[index] == 'undefined') {
    // does not exist
  }
  else {
  // does exist
  }
} 
catch (error){ /* ignore */ }

...无论如何,这就是它在chrome中的工作方式(否则,代码会因错误而停止)。

你可以简单地使用这个:

var tmp = ['a', 'b'];
index = 3 ;
if( tmp[index]){
    console.log(tmp[index] + '\n');
}else{
    console.log(' does not exist');
}

这些天,我会利用ecmascript,像这样使用它

return myArr?.[index]
if(typeof arrayName[index] == undefined) {
    console.log("Doesn't exist")
}
else {
console.log("does exist")
}