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
lodash: check object is empty
提问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 false
result, 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/