如何检查数组元素是否存在或不在JavaScript中?

我正在与钛,

我的代码看起来像,

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
}

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

if (myArray.indexOf(searchTerm) === -1) {
  console.log("element doesn't exist");
}
else {
  console.log("element found");
}

考虑数组a:

var a ={'name1':1, 'name2':2}

如果您想检查“名1”的存在,简单地测试它in

if('name1' in a){
console.log('name1 exists in a')
}else
console.log('name1 is not in a')
链接地址: http://www.djcxy.com/p/26641.html

上一篇: How to check if array element exists or not in javascript?

下一篇: How to create a hash or dictionary object in JavaScript