如果某个值存在于某个数组索引处,我如何检查 JavaScript? [英] How do I check in JavaScript if a value exists at a certain array index?

查看:24
本文介绍了如果某个值存在于某个数组索引处,我如何检查 JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否适用于测试位置 index 处的值是否存在,或者是否有更好的方法:

Will this work for testing whether a value at position index exists or not, or is there a better way:

if(arrayName[index]==""){
     // do stuff
}

推荐答案

从概念上讲,JavaScript 中的数组包含 array.length 元素,从 array[0] 开始直到array[array.length - 1].如果 i 介于 0array.length - 1<之间,则索引为 i 的数组元素被定义为数组的一部分/code> 包括在内.如果 i 不在此范围内,则它不在数组中.

Conceptually, arrays in JavaScript contain array.length elements, starting with array[0] up until array[array.length - 1]. An array element with index i is defined to be part of the array if i is between 0 and array.length - 1 inclusive. If i is not in this range it's not in the array.

所以从概念上讲,数组是线性的,从零开始到最大值,没有任何机制可以在不存在条目的范围内产生间隙".要找出给定位置索引处是否存在值(其中索引为 0 或正整数),您实际上只需使用

So by concept, arrays are linear, starting with zero and going to a maximum, without any mechanism for having "gaps" inside that range where no entries exist. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use

if (i >= 0 && i < array.length) {
  // it is in array
}

现在,在幕后,JavaScript 引擎几乎肯定不会像这样线性和连续地分配数组空间,因为它在动态语言中没有多大意义,并且对于某些代码来说效率低下.它们可能是哈希表或某种混合策略,并且未定义的数组范围可能没有分配到自己的内存.尽管如此,JavaScript 语言希望将 array.length n 数组呈现为具有 n 个成员,并且它们被命名为 0n - 1,这个范围内的任何东西都是数组的一部分.

Now, under the hood, JavaScript engines almost certainly won't allocate array space linearly and contiguously like this, as it wouldn't make much sense in a dynamic language and it would be inefficient for certain code. They're probably hash tables or some hybrid mixture of strategies, and undefined ranges of the array probably aren't allocated their own memory. Nonetheless, JavaScript the language wants to present arrays of array.length n as having n members and they are named 0 to n - 1, and anything in this range is part of the array.

然而,您可能想要的是知道数组中的值是否实际上是已定义的 - 也就是说,它不是 undefined.也许您甚至想知道它是否已定义并且不是 null.可以在不设置成员值的情况下向数组添加成员:例如,如果您通过增加 array.length 属性来添加数组值,则任何新值都将是 undefined.

What you probably want, however, is to know if a value in an array is actually something defined - that is, it's not undefined. Maybe you even want to know if it's defined and not null. It's possible to add members to an array without ever setting their value: for example, if you add array values by increasing the array.length property, any new values will be undefined.

确定给定的值是否有意义,或已定义.也就是说,not undefined,或者 null:

To determine if a given value is something meaningful, or has been defined. That is, not undefined, or null:

if (typeof array[index] !== 'undefined') {

if (typeof array[index] !== 'undefined' && array[index] !== null) {

有趣的是,由于 JavaScript 的比较规则,我的最后一个示例可以优化为:

Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to this:

if (array[index] != null) {
  // The == and != operators consider null equal to only null or undefined
}  

这篇关于如果某个值存在于某个数组索引处,我如何检查 JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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