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

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

    This way is easiest one in my opinion.

    var nameList = new Array('item1','item2','item3','item4');
    
    // Using for loop to loop through each item to check if item exist.
    
    for (var i = 0; i < nameList.length; i++) {
    if (nameList[i] === 'item1') 
    {   
       alert('Value exist');
    }else{
       alert('Value doesn\'t exist');
    }
    

    And Maybe Another way to do it is.

    nameList.forEach(function(ItemList)
     {
       if(ItemList.name == 'item1')
            {
              alert('Item Exist');
            }
     }
    

提交回复
热议问题