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


当前回答

如果你在找这样的东西。

下面是摘录

var demoArray = ['A','B','C','D']; var ArrayIndexValue = 2; 如果(demoArray.includes (ArrayIndexValue)) { 警报(“存在的价值”); //数组索引存在 其他}{ Alert(“不存在”); //数组索引不存在 }

其他回答

如果数组的元素也是简单对象或数组,你可以使用一些函数:

// search object
var element = { item:'book', title:'javasrcipt'};

[{ item:'handbook', title:'c++'}, { item:'book', title:'javasrcipt'}].some(function(el){
    if( el.item === element.item && el.title === element.title ){
        return true; 
     } 
});

[['handbook', 'c++'], ['book', 'javasrcipt']].some(function(el){
    if(el[0] == element.item && el[1] == element.title){
        return true;
    }
});

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

(typeof files[1] === undefined)?
            this.props.upload({file: files}):
            this.props.postMultipleUpload({file: files widgetIndex: 0, id})

使用typeof检查数组中的第二项是否为undefined,并检查是否为undefined

简单的方法来检查项目是否存在

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--)
       if (this[i] == obj)
       return true;
    return false;
}

var myArray= ["Banana", "Orange", "Apple", "Mango"];

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