如何在javascript中检查数组元素是否存在? [英] How to check if array element exists or not in javascript?

查看:187
本文介绍了如何在javascript中检查数组元素是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Titanium,我的代码如下所示:

I am working with Titanium, my code looks like this:

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 。我仍然无法使用上面的代码检测到不存在的元素。

I am passing an index to the array currentData. I am still not able to detect a non-existing element using above code.

推荐答案

使用 typeof arrayName [index] ==='undefined'

ie

if(typeof arrayName[index] === 'undefined') {
    // does not exist
}
else {
    // does exist
}

这篇关于如何在javascript中检查数组元素是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆