如何在javascript中检查数组元素是否存在?

Pra*_*eep 167 javascript titanium titanium-mobile

我正在使用Titanium,我的代码看起来像这样:

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;
}
Run Code Online (Sandbox Code Playgroud)

我正在向数组传递一个索引currentData.我仍然无法使用上面的代码检测到不存在的元素.

tec*_*bar 334

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

if(typeof arrayName[index] === 'undefined') {
    // does not exist
}
else {
    // does exist
}
Run Code Online (Sandbox Code Playgroud)

  • @AnchovyLegend没有,你不能!但是你可以使用`if(arrayName [index] === undefined)`. (60认同)
  • 如果项目在那里,则失败,但它的值是未定义的; 请改用此答案 - > http://stackoverflow.com/questions/1098040/checking-if-an-array-key-exists-in-a-javascript-object-or-array (15认同)
  • +1,很好.您也可以使用`if(arrayName [index] ==='undefined')`作为快捷方式 (4认同)
  • 对于 if(arrayName[index] === undefined) 你可以使用更短的 if(!arrayName[index]) (2认同)

yes*_*nth 79

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

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

  • 在我看来这是最好的答案,现在IE 7不再受到控制,所以这不是问题.虽然我建议使用三等于`if(myArray.indexOf(searchTerm)=== -1)` (4认同)
  • 不幸的是,这个在IE 7及以下版本中不起作用. (2认同)
  • OP正在查看给定的** index **号是否存在。这是在检查给定的**值**是否存在。 (2认同)

r3w*_*3wt 16

如果我错了,请有人纠正我,但AFAIK以下是正确的:

  1. 数组实际上只是 JS 引擎盖下的对象
  2. 因此,他们有原型方法 hasOwnProperty “继承”自Object
  3. 在我的测试中, hasOwnProperty 可以检查数组索引处是否存在任何内容。

因此,只要上述情况属实,您可以简单地:

const arrayHasIndex = (array, index) => Array.isArray(array) && array.hasOwnProperty(index);

用法:

arrayHasIndex([1,2,3,4],4); 输出: false

arrayHasIndex([1,2,3,4],2); 输出: true

  • 这也适用于数组中的未定义值和空值,这里的其他答案都没有这样做。 (4认同)
  • 从MDN验证:“如果一个Object是一个Array,hasOwnProperty方法可以检查索引是否存在。” https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty (2认同)
  • 另外,“hasOwnProperty”对于像“__proto__”这样的继承属性不会返回 true,这应该是答案。 (2认同)

ᆼᆺᆼ*_*ᆼᆺᆼ 9

这正是in运营商的用途。像这样使用它:

if (index in currentData) 
{ 
    Ti.API.info(index + " exists: " + currentData[index]);
}
Run Code Online (Sandbox Code Playgroud)

接受的答案是错误的,它会给出假阴性如果在值indexundefined

const currentData = ['a', undefined], index = 1;

if (index in currentData) {
  console.info('exists');
}
// ...vs...
if (typeof currentData[index] !== 'undefined') {
  console.info('exists');
} else {
  console.info('does not exist'); // incorrect!
}
Run Code Online (Sandbox Code Playgroud)


Sau*_*ius 7

这些天我会利用 ecmascript 并像那样使用它

return myArr?.[index]
Run Code Online (Sandbox Code Playgroud)


NVR*_*VRM 6

===这也可以很好地工作,使用against进行类型测试undefined

if (array[index] === undefined){ return } // True
Run Code Online (Sandbox Code Playgroud)

测试:

if (array[index] === undefined){ return } // True
Run Code Online (Sandbox Code Playgroud)

或者类似地:

const fruits = ["Banana", "Orange", "Apple", "Mango"];

if (fruits["Cherry"] === undefined){
  console.log("There isn't any cherry in the fruits basket :(")
}
Run Code Online (Sandbox Code Playgroud)


Swi*_*ter 5

我不得不将 techfoobar 的答案包装在一个try..catch块中,如下所示:

try {
  if(typeof arrayName[index] == 'undefined') {
    // does not exist
  }
  else {
  // does exist
  }
} 
catch (error){ /* ignore */ }
Run Code Online (Sandbox Code Playgroud)

...无论如何,这就是它在 chrome 中的工作方式(否则,代码会因错误而停止)。

  • 如果变量“arrayName”本身(或“index”)不存在,这应该只会“损坏”并出现错误。简单地访问未定义的数组元素不应该导致“错误”? (2认同)