How to check if array element exists or not in javascript?

前端 未结 18 1696
粉色の甜心
粉色の甜心 2020-12-07 10:05

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

var currentData = new Array();

if(currentData[index]!==\"\"||currentData[index]!==null||currentData[ind         


        
18条回答
  •  粉色の甜心
    2020-12-07 10:46

    When trying to find out if an array index exists in JS, the easiest and shortest way to do it is through double negation.

    let a = [];
    a[1] = 'foo';
    console.log(!!a[0])   // false
    console.log(!!a[1])   // true
    

提交回复
热议问题