繁体   English   中英

如果某个值存在于某个数组索引处,我如何检查 JavaScript?

[英]How do I check in JavaScript if a value exists at a certain array index?

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

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

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

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

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

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

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

确定给定的值是否有意义,或已被定义。 也就是说,不是undefinednull

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

或者

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

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

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

我们不能这样做:

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}

仅使用.length是不安全的,并且会在某些浏览器中导致错误。 这是一个更好的解决方案:

if(array && array.length){   
   // not empty 
} else {
   // empty
}

或者,我们可以使用:

Object.keys(__array__).length
if(!arrayName[index]){
     // do stuff
}
if(arrayName.length > index && arrayName[index] !== null) {
    //arrayName[index] has a value
}

简短而通用的方法

如果你想检查任何数组是否有假值(比如 false、undefined、null 或空字符串),你可以像这样使用every()方法:

array.every(function(element) {return !!element;}); // returns true or false

例如:

['23', null, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', '', 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', true, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns true

如果您需要获取虚假值的第一个索引,您可以这样做:

let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3

如果您只需要检查给定索引的数组的虚假值,您可以这样做:

if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if(typeof arr ==='object' && arr instanceof Array ){
   if(!arr.length){
      println 'empty'
   }else{
      printn 'not Empty'
   }

}else{
   println 'Null'
}

如果您的意思是 'Null' -> 它的元素为 null 或等于 '' ,在这种情况下:在过滤所有 'null' 元素后检查数组是否为空

if(!arr.clean().length){return 'is null'}

当然,之前添加Clean方法:

Array.prototype.clean=function(){return this.filter(function(e){return (typeof  e !=='undefined')&&(e!= null)&&(e!='')})}

这取决于你对“空”的意思。

当您尝试获取没有具有该名称的属性的对象的属性值时,您将获得值undefined

这就是稀疏数组会发生的情况:并非0array.length-1之间的所有索引都存在。

所以你可以检查array[index] === undefined

但是,属性index可能存在undefined值。 如果要过滤掉这种情况,可以使用in运算符或hasOwnProperty ,如如何检查对象在 JavaScript 中是否具有属性中所述?

index in array;
array.hasOwnProperty(index);

如果您想将具有undefinednull值的现有属性视为不存在,您可以使用松散比较array[index] == undefinedarray[index] == null

如果您知道数组不是稀疏的,您可以将indexarray.length进行比较。 但为了安全起见,您可能需要确保index确实是数组索引,请参阅检查属性名称是否为数组索引

我建议创建一个这样的函数:

function isEmptyEl(array, i) {
   return !(array[i]);
}

你可以这样称呼它:

if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}

强制开发人员遵守 isEmptyEl 接口将捕获输入错误,例如未定义的 arrayName 或 indexVal 变量。

(在使用 Javascript 编程时进行防御性编程通常是一种很好的做法。)

如果未定义 arrayName,您将收到这样的错误:

Uncaught ReferenceError: arrayName is not defined
    at <anonymous>:2:15
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

未定义的 indexVal 的类似结果。

如果数组或索引值不存在,则会出现错误。

对于有效输入,只有当 arrayName[indexVal] 是以下任何一项时,您才会得到 true:

  • 空值
  • 不明确的
  • NaN
  • 空字符串
  • 0
  • 错误的

我想指出一些似乎遗漏的东西:即可能在数组中间有一个“空”数组位置。 考虑以下:

let arr = [0, 1, 2, 3, 4, 5]

delete arr[3]

console.log(arr)      // [0, 1, 2, empty, 4, 5]

console.log(arr[3])   // undefined

检查的自然方法是查看数组成员是否未定义,我不确定是否存在其他方法

if (arr[index] === undefined) {
  // member does not exist
}

好的,让我们首先看看如果 JavaScript 中不存在数组值会发生什么,所以如果我们有一个像下面这样的数组:

const arr = [1, 2, 3, 4, 5];

现在我们检查6是否在索引5 处

arr[5];

我们得到undefined ...

所以这基本上给了我们答案,检查是否未定义的最佳方法,所以是这样的:

if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}

在这种情况下最好要这样做:

if(!arrayName[index]) {
  //Don't check like this..
}

因为假设我们有这个数组:

const arr = [0, 1, 2];

我们这样做:

if(!arr[0]) {
  //This get passed, because in JavaScript 0 is falsy
}

因此,如您所见,即使是0 ,也不会被识别,很少有其他事情可以做同样的事情并使您的应用程序出错,所以要小心,我将它们全部列出:

  1. 未定义:如果没有定义的值,它的undefined
  2. null : 如果它是 null,例如如果 DOM 元素不存在...
  3. 空字符串''
  4. 0 : 数字零
  5. NaN : 不是数字
  6. 错误的

真实检测: in运营商

这个问题的年龄大约是 10 年,令人惊讶的是还没有人提到这一点 - 但是当我们使用delete操作符时,有些人会看到这个问题(例如这里)。 这也是一个有点反直觉的解决方案,但是在“对象世界”中工作的in运算符也可以与数组一起工作(因为我们可以像查看“键”一样查看数组索引...)。 通过这种方式,我们可以检测并区分undefined数组值和delete值(索引)

if(index in arrayName) {
   // do stuff 
}

 let arr = [0, 1, 2, 3, null, undefined,6] delete arr[2]; // we delete element at index=2 if(2 in arr) console.log('You will not see this because idx 2 was deleted'); if(5 in arr) console.log('This is element arr[5]:', arr[5]); // Whole array and indexes bigger than arr.length: for(let i=0; i<=9; i++) { let val = (i in arr) ? arr[i] : 'empty' let bound = i<arr.length ? '' : '(out of range)' console.log(`${i} value: `, val, bound); } console.log('Look on below aray on chrome console (not in SO snippet console)'); console.log('typeof arr:', typeof arr); console.log(arr);

Chrome 控制台显示了有关已删除索引 2 的代码段数组的一些信息 - 该索引实际上根本不存在(!!!)(与从对象中删除键的方式相同)。 这里也有趣的是数组被视为键值对(我们甚至看到“长度”键)。 有趣的是, typeof arr是 Object (!!!), deletein运算符的工作方式类似于 JS 对象(方括号表示法arr[idx]obj[key]也类似) - 所以它看起来像数组核心中的特殊 JS 对象。

在此处输入图片说明

为了在不delete情况下获得类似的效果,定义数组如下

[0, 1,, 3, null, undefined, 6] // pay attention to double comma: ",,"

如果 array[index] 为空,试试这个

if (array[index] != null) 

使用 Lodash,您可以:

if(_.has(req,'documents')){
      if (req.documents.length)
      _.forEach(req.documents, function(document){
        records.push(document);
      });
} else {
}

if(_.has(req,'documents'))是检查我们的请求对象是否有一个名为documents的属性,如果它有这个属性,下一个if (req.documents.length)是验证它是否不是一个空数组,所以像forEach这样的其他东西可以继续。

要检查它是否从未被定义或是否已被删除:

if(typeof arrayName[index]==="undefined"){
     //the index is not in the array
}

也适用于关联数组和删除了一些索引的数组

要检查它是否从未被定义、被删除或是否为空或逻辑空值(NaN、空字符串、false):

if(typeof arrayName[index]==="undefined"||arrayName[index]){
     //the index is not defined or the value an empty value
}

我使用 Laravel 数据表遇到了这个问题。 我在活动日志中存储了一个名为properties的 JSON 值,并希望根据该值是否为空来显示一个按钮。

好吧,如果它是空的,则数据表将其解释为数组,如果不是,则将其解释为对象,因此,以下解决方案对我有用:

render: function (data, type, full) {
    if (full.properties.length !== 0) {
        // do stuff
    }
}

对象没有长度属性。

我认为这个决定适合那些更喜欢声明式函数式编程而不是命令式 OOP 或过程式编程的人。 如果您的问题是里面是否有一些值? (真值或假值)”,您可以使用.some方法来验证里面的值。

[].some(el => el || !el);
  • 它并不完美,但不需要应用任何包含相同逻辑的额外函数,例如function isEmpty(arr) { ... }
  • 它仍然比“它是零长度吗?”听起来更好 当我们这样做时, [].length结果为0 ,这在某些情况下是危险的。
  • 甚至这个[].length > 0“它的长度是否大于零?”

高级示例:

[    ].some(el => el || !el); // false
[null].some(el => el || !el); // true
[1, 3].some(el => el || !el); // true

当您创建空数组值时,不是未定义的,它们是空的

var arr = new Array(10); // (10) [empty × 10]

但是当您按索引获取项目时会收到​​未定义

arr[0]; // undefined

所以你不能通过 === 比较它们是未定义的还是空的

我们可以通过使用 JSON.stringify 来实现,转换整个数组它用 null 替换空值

JSON.stringify(arr); // "[null,null,null,null,null,null,null,null,null,null]"
JSON.stringify(arr[0]); // but for single item it returns undefined

所以真正检查空值:

arr[0] !== null && JSON.stringify(arr.map(item => !item)).slice(1, -1).split(',')[0] === 'null'
  1. 将 item 与 null 进行比较(应该不相等)
  2. 映射数组删除false而不是现有值(确保字符串化结构以逗号分隔,没有多余的逗号)
  3. JSON.stringify 数组
  4. 从字符串中删除括号
  5. 用逗号分割成数组
  6. null比较

您可以使用 Loadsh 库更有效地执行此操作,例如:

如果您有一个名为“pets”的数组,例如:

var pets = ['dog', undefined, 'cat', null];

console.log(_.isEmpty(pets[1])); // true
console.log(_.isEmpty(pets[3])); // true
console.log(_.isEmpty(pets[4])); // false

_.map( pets, (pet, index) => { console.log(index + ': ' + _.isEmpty(pet) ) });

要检查所有数组值是否为空或未定义值:

 var pets = ['dog', undefined, 'cat', null]; console.log(_.isEmpty(pets[1])); // true console.log(_.isEmpty(pets[3])); // true console.log(_.isEmpty(pets[4])); // false _.map( pets, (pet, index) => { console.log(index + ': ' + _.isEmpty(pet) ) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

http://underscorejs.org/查看更多示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM