Javascript 检查空地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12057527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:01:17  来源:igfitidea点击:

Check for an empty map

javascript

提问by chama

I'm having an interesting issue that I'm sure is easily explained, but the explanation is eluding me.

我遇到了一个有趣的问题,我确信它很容易解释,但我无法解释。

An undefined or null object in javascript is equal to false.

javascript 中的未定义或空对象等于 false。

var x;
alert(!x); //returns true
alert(x==true); //returns false

What about an empty array object? Is that the equivalent of true or false?

空数组对象呢?这相当于真还是假?

var x = [];
alert (x==true); //returns false
alert (!x); //returns false

If it is equivalent to true, how do I check if it's non-empty? I was hoping to do

如果它等价于 true,我如何检查它是否为非空?我希望做

if (!x) {
    //do stuff
}

I tried checking x.length, but I'm using this object as a map:

我尝试检查x.length,但我将此对象用作地图:

var x = [];
alert(x.length); //returns 0
x.prop = "hello"; 
alert(x.length); //still returns 0

How can I check if my map is empty?

如何检查我的地图是否为空?

采纳答案by Hyman Franklin

Little confused as you seem to be mixing objects and arrays in your question. Hopefully this might help clear it up for you.

有点困惑,因为您似乎在问题中混合了对象和数组。希望这可以帮助您清除它。

An empty array evaluates to true:

空数组的计算结果为 true:

!![] //true

But integer 0 evaluates to false, so you can do:

但是整数 0 的计算结果为 false,因此您可以执行以下操作:

!![].length //false

So:

所以:

if([].length) {
   //array has 1 element at least
} else {
  //array has 0 elements
}

However you do seem to be getting arrays and objects confused. In JavaScript, we have objects:

但是,您似乎确实混淆了数组和对象。在 JavaScript 中,我们有对象:

var x = {};
x.foo = "bar";
x.baz = 2;

And we have arrays:

我们有数组:

var x = [];
x.push("foo");
x.length //1

You can't do what you do in your opener:

你不能做你在开场白中做的事情:

var x = []; //x is an array
x.foo = "bar"; //can't do this on an array
x.length; // 0 

回答by Jo?o Silva

It's not as easy as it looks, you have to check that the object has at least one property.

这并不像看起来那么容易,您必须检查对象是否至少具有一个属性。

jQuery provides isEmptyObjectfor that purpose:

jQueryisEmptyObject为此目的提供:

function isEmptyObject( obj ) {
    for ( var name in obj ) {
        return false;
    }
    return true;
}

Sample usage:

示例用法:

> var x = [];
> x.prop = "hello"; 
> isEmptyObject(x);
false

回答by wannas

if you are using JQuery

如果您使用的是 JQuery

jQuery.isEmptyObject(obj)

If not, u can implement your own

如果没有,你可以实现自己的

isEmptyObject = function(obj) {
for (key in obj) {
    if (obj.hasOwnProperty(key)) return false;
}
return true;
};

回答by Bikush

First of all, you are using an Array not a map. A map would be an Object instance:

首先,您使用的是数组而不是地图。地图将是一个 Object 实例:

var x = {};
x.prop = "5";

Your code is valid since Arrays are also Objects. You can check if your Object is empty as answered in this question.

您的代码是有效的,因为数组也是对象。您可以检查您的对象是否为空,如本问题所述

回答by Decoded

If you're using a Proper JS Map<K, V>Object, you can simply use the Map API on Mozilla Docs.

如果您使用的是合适的 JSMap<K, V>对象,您可以简单地使用Mozilla Docs上的 Map API 。

Specifically there is a Map.prototype.sizeproperty, as well as Map.prototype.entries()method, which I believe returns an Array []of keys. If that is length 0then the map is empty.

具体来说,有一个Map.prototype.size属性和Map.prototype.entries()方法,我相信它会返回一个[]键数组。如果是,length 0那么地图是空的。

回答by Y2H

*If of an empty list will give true (if (array1)).

*如果一个空列表将给出真(if (array1))。

*x.propdoes not really add any elements to the array, it just assigns value "hello" to property x of the array but the array is still empty. So you should still use .lengthto check is array is empty.

*x.prop并没有真正向数组添加任何元素,它只是将值“hello”分配给数组的属性 x 但数组仍然是空的。所以你仍然应该使用.length检查数组是否为空。