简体   繁体   English

为什么我不能使用 array == [] 检查数组是否为空?

[英]Why can't I check if an array is empty with array == []?

This is a question from CodeWars that is named "Count of positives/sum of negatives".这是来自 CodeWars 的一个名为“正数计数/负数总和”的问题。 It says:它说:

If the input array is empty or null, return an empty array如果输入数组为空或null,则返回一个空数组

To check if the array is empty, I decided to check if it was an empty array.为了检查数组是否为空,我决定检查它是否为空数组。 When I try to do:当我尝试这样做时:

if(input == [])

I fail the test, but if I do:我没有通过测试,但如果我这样做:

if(input.length == 0)

I pass the test.我通过了测试。 An empty array should be equal to [] right?一个空数组应该等于[]对吗? Why is there a difference, and what is the difference between these two checks?为什么会有区别,这两个检查有什么区别?

My code is as follows:我的代码如下:

function countPositivesSumNegatives(input) {
   var a = 0;
   var b = 0;
   if (input == null) {
      return []
   }
   if (input.length == 0) {
      return []
   }
   for (var i = 0; i < input.length; i++) {
      if (input[i] > 0) {
         a++;
      }
      if (input[i] < 0) {
         b += input[i];
      }
   }
   return [a, b]
}

The problem is that arrays are objects .问题是数组是对象 When you compare two objects, you compare their references.比较两个对象时,是在比较它们的引用。 Per the MDN documentation :根据MDN 文档

Equality (==)平等(==)

If both operands are objects , then JavaScript compares internal references which are equal when operands refer to the same object in memory.如果两个操作数都是对象,那么当操作数引用内存中的同一个对象时,JavaScript 会比较相等的内部引用。

Since two instances of arrays don't necessarily have the same reference, the check fails.由于数组的两个实例不一定具有相同的引用,因此检查失败。 Just try this in the console:只需在控制台中尝试此操作:

> [] == []
false

Two arrays seemingly with the same content (or lack thereof) are not equal.看似具有相同内容(或缺少内容)的两个数组并不相等。 That's because content is not checked, but reference.那是因为没有检查内容,而是参考。 These two arrays are separate instances and refer to different places in memory, thus the check evaluates to false.这两个数组是独立的实例并且引用内存中的不同位置,因此检查评估为假。

On the other hand, just checking the length, and if it is zero checks if the array is empty or not.另一方面,只检查长度,如果它为零,则检查数组是否为空。 The length property signifies the amount of content in an array 1 and is part of every array. length属性表示数组1中的内容量,并且是每个数组的一部分。 Since it is part of every array and reflects the amount of data in the array, you can use it to check if the array is empty or not.由于它是每个数组的一部分,并且反映了数组中的数据量,因此您可以使用它来检查数组是否为空。


1 Beware, though, of sparse arrays as mentioned by RobG in the comments. 1不过,请注意RobG在评论中提到的稀疏数组 Such arrays can be created with new Array(N) which will give you an empty array, but with length N.这样的数组可以用new Array(N)创建,它会给你一个空数组,但长度为 N。

null has no length null 没有长度

  if (input === null || input.length === 0 ) {
    return [];
    }
 int[] res2 = new int[] { };

 if (input == null || input.Length == 0)
       return res2;

This code will solve your problem此代码将解决您的问题

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

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