Javascript lodash:检查对象是否为空

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

lodash: check object is empty

javascriptlodash

提问by Meldum

I have this object: {"": undefined}

我有这个对象: {"": undefined}

and when I check this object for empty in this way: _.isEmpty({"": undefined})

当我以这种方式检查这个对象是否为空时: _.isEmpty({"": undefined})

I get falseresult, maybe in lodash we have another method?

我得到了false结果,也许在 lodash 中我们有另一种方法?

采纳答案by Paul S.

Your example object is notemptyso instead perhaps you want to test if all properties are undefined

你例子中的物体没有所以不是也许你想测试,如果所有属性未定义

let o = {foo: undefined};
!_.values(o).some(x => x !== undefined); // true

回答by Armen Zakaryan

_.isEmpty(obj, true)

var obj = {
  'firstName': undefined
, 'lastName' : undefined
};

console.log(_.isEmpty(obj)); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/

请参阅http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/

回答by pkerckhove

It depends on how you want to check it. Do you want to check someor every

这取决于您想如何检查它。你想检查一些还是每一个

Then what you can do is :

那么你可以做的是:

import { some, isEmpty } from 'lodash'
console.log(some(this.yourObject, isEmpty))