简体   繁体   English

为什么你可以检查Javascript中是否存在.length的数组

[英]Why you can check if array exists in Javascript with .length

As I Java programmer I do not understand why you can check if the array is empty using if(array.length) . 作为Java程序员,我不明白为什么你可以使用if(array.length)检查数组是否为空。 Can someone please elaborate? 有人可以详细说明吗?

because first if array is not defined (undefined) then you can't get the length property of an undefined object. 因为首先如果未定义array (未定义),则无法获取未定义对象的length属性。

just use if (typeof array !== 'undefined') will give the result if array exsit. 只要使用if (typeof array !== 'undefined')将在数组exsit时给出结果。

as you can see in the example code: 正如您在示例代码中看到的:

zero.length --> 0
!zero.length --> true
!!zero.length --> false

0 is considered as false in if(0) or !!0 / !0 , since an empty array have length of 0, it is considered as false, in that case you can't use it to determine if this array exsit or it is just empty . if(0)!!0 / !0 0被视为false,因为空数组的长度为0,它被认为是false, 在这种情况下你不能用它来确定这个数组是否存在或者它是只是空的

 var array = []; if (typeof array !== 'undefined'){ console.log('array exsit: true'); } else { console.log('array exsit: false'); } var zero = []; console.log('zero.length --> '+zero.length); console.log('!zero.length --> '+!zero.length); console.log('!!zero.length --> '+!!zero.length); if (typeof array2 !== 'undefined'){ console.log('array2 exsit: true'); } else { console.log('array2 exsit: false'); } 

below is the table for js truthy and falsy 下面是js truthyfalsy

 function checkTruthiness(raw, exp) { row = $('<tr />').appendTo('#output'); if ( exp ) { row.append('<td class="truthy">' + raw + '</td>'); } else { row.append('<td class="falsy">' + raw + '</td>'); } if ( !exp ) { row.append('<td class="truthy"> !' + raw + '</td>'); } else { row.append('<td class="falsy"> !' + raw + '</td>'); } if ( exp == true ) { row.append('<td class="truthy">' + raw + ' == true </td>'); } else { row.append('<td class="falsy">' + raw + ' == true </td>'); } if ( exp == false ) { row.append('<td class="truthy">' + raw + ' == false </td>'); } else { row.append('<td class="falsy">' + raw + ' == false </td>'); } if ( exp === true ) { row.append('<td class="truthy">' + raw + ' === true </td>'); } else { row.append('<td class="falsy">' + raw + ' === true </td>'); } if ( exp === false ) { row.append('<td class="truthy">' + raw + ' === false </td>'); } else { row.append('<td class="falsy">' + raw + ' === false </td>'); } if ( Boolean(exp) ) { row.append('<td class="truthy">Boolean(' + raw + ')</td>'); } else { row.append('<td class="falsy">Boolean(' + raw + ')</td>'); } } checkTruthiness('0', 0); checkTruthiness('NaN', NaN); checkTruthiness('""', ""); checkTruthiness('false', false); checkTruthiness('null', null); checkTruthiness('undefined', undefined); checkTruthiness('{}', {}); checkTruthiness('[]', []); checkTruthiness('"0"', "0"); checkTruthiness('"false"', "false"); 
 td { font-family: Verdana, sans-serif; font-size:13px; padding:5px 10px; } .truthy { background-color:#cfc; } .falsy { background-color:#fcc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="output"></table> 

Actually array.length is evaluated as true when it has at least one element. 实际上,当array.length至少有一个元素时,它被评估为true When array is empty array.length returns 0 which is evaluated as false . 当array为空时, array.length返回0 ,其值为false

So the bottom line is you can use if (array.length) to check if array is NOT empty. 所以底线是你可以使用if (array.length)来检查数组是否为空。

array.length will return 0 if the array is empty, 0 is a falsy value. array.length将返回0,如果数组为空, 0是一个falsy值。

Falsy values 虚假的价值观

Examples of falsy values in JavaScript (which translate to false and thus bypass the if block): JavaScript中的虚假值的示例(转换为false,从而绕过if块):

  • false
  • 0 (zero) 0 (零)
  • "" (empty string) "" (空字符串)
  • null
  • undefined
  • NaN

So 所以

var arr = [];

arr.length === 0
arr.length == false
!arr.length === true

Any non-zero number is a truthy value, and will enter the if block. 任何非零数字都是值,并将进入if块。

零强制为false

 if (!0) console.log(true); 

You can dynamically calculate the number of elements in the array through the lenght property (the same property can also be applied to a string to count its characters, in the case of the Array () the number of indexes is calculated). 您可以通过lenght属性动态计算数组中元素的数量(同样的属性也可以应用于字符串以计算其字符,在Array()的情况下,计算索引的数量)。 Since .lenght returns the number of array elements, if it is empty you will have 0 as the return value. 由于.lenght返回数组元素的数量,如果它是空的,则返回值为0。

In most of the programming languages, 0 and null ( undefined in case of JavaScript) are equivalent to false . 在大多数编程语言中, 0null (在JavaScript的情况下undefined )等同于false And thus that if condition is correct. 因此, if条件是正确的。

Unless you want totally different behavior for 0 and null , then you'll have to do check using == ( === in case of JS). 除非你想要0null完全不同的行为,那么你将不得不使用=====在JS的情况下)进行检查。 This statement returns boolean value. 该语句返回布尔值。

JavaScript considers NaN & Empty string also as false. JavaScript认为NaN和Empty字符串也为false。

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

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