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

前端 未结 18 1725
粉色の甜心
粉色の甜心 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:31

    If elements of array are also simple objects or arrays, you can use some function:

    // 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;
        }
    });
    

提交回复
热议问题