如何在 Javascript 中匹配空字典?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6072590/
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 20:07:33  来源:igfitidea点击:

How to match an empty dictionary in Javascript?

javascriptdictionarymatch

提问by Jo?o Pinto Jerónimo

From the node REPL thing,

从节点 REPL 的事情,

> d = {}
{}
> d === {}
false
> d == {}
false

Given I have an empty dictionary, how do I make sure it is an empty dictionary ?

鉴于我有一本空字典,我如何确保它是一本空字典?

回答by Raynos

function isEmpty(obj) {
  return Object.keys(obj).length === 0;
}

回答by Gumbo

You could extend Object.prototypewith this isEmptymethod to check whether an object has no own properties:

您可以Object.prototype使用此isEmpty方法扩展以检查对象是否没有自己的属性:

Object.prototype.isEmpty = function() {
    for (var prop in this) if (this.hasOwnProperty(prop)) return false;
    return true;
};

回答by stroz

How about using jQuery?

使用 jQuery 怎么样?

$.isEmptyObject(d)

回答by David Ruttka

Since it has no attributes, a forloop won't have anything to iterate over. To give credit where it's due, I found this suggestion here.

由于它没有属性,for循环将没有任何可迭代的内容。为了给予应有的信任,我在这里找到了这个建议。

function isEmpty(ob){
   for(var i in ob){ return false;}
  return true;
}

isEmpty({a:1}) // false
isEmpty({}) // true

回答by cevaris

This is what jQueryuses, works just fine. Though this does require the jQuery script to use isEmptyObject.

这就是jQuery使用的,工作得很好。虽然这确实需要 jQuery 脚本使用isEmptyObject

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

//Example
var temp = {};
$.isEmptyObject(temp); // returns True
temp ['a'] = 'some data';
$.isEmptyObject(temp); // returns False

If including jQuery is not an option, simply create a separate pure javascript function.

如果包含 jQuery 不是一个选项,只需创建一个单独的纯 javascript 函数。

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

//Example
var temp = {};
isEmptyObject(temp); // returns True
temp ['b'] = 'some data';
isEmptyObject(temp); // returns False

回答by Steve Bennett

I'm far from a JavaScript scholar, but does the following work?

我远非 JavaScript 学者,但以下方法有效吗?

if (Object.getOwnPropertyNames(d).length == 0) {
   // object is empty
}

It has the advantage of being a one line pure function call.

它的优点是单行纯函数调用。

回答by VAMSHI PAIDIMARRI

var SomeDictionary = {};
if(jQuery.isEmptyObject(SomeDictionary))
// Write some code for dictionary is empty condition
else
// Write some code for dictionary not empty condition

This Works fine.

这工作正常。

回答by Steve Bennett

If performance isn't a consideration, this is a simple method that's easy to remember:

如果不考虑性能,这是一个简单易记的方法:

JSON.stringify(obj) === '{}'

Obviously you don't want to be stringifying large objects in a loop, though.

不过,显然您不想在循环中对大对象进行字符串化。

回答by chrisf

You'd have to check that it was of type 'object' like so:

您必须检查它是否为“对象”类型,如下所示:

(typeof(d) === 'object')

(typeof(d) === 'object')

And then implement a short 'size' function to check it's empty, as mentioned here.

然后实现一个简短的“大小”函数来检查它是否为空,如此处所述

回答by arbyter

If you try this on Node.js use this snippet, based on this code here

如果您在 Node.js 上尝试此操作,请使用此代码段,基于此处的代码

Object.defineProperty(Object.prototype, "isEmpty", {
    enumerable: false,
    value: function() {
            for (var prop in this) if (this.hasOwnProperty(prop)) return false;
            return true;
        }
    }
);