如何使用 lodash 从对象中删除未定义和空值? [英] How to remove undefined and null values from an object using lodash?
本文介绍了如何使用 lodash 从对象中删除未定义和空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Javascript 对象,例如:
I have a Javascript object like:
var my_object = { a:undefined, b:2, c:4, d:undefined };
如何删除所有未定义的属性?虚假属性应该保留.
How to remove all the undefined properties? False attributes should stay.
推荐答案
如果你想删除所有falsey values 那么最紧凑的方式是:
If you want to remove all falsey values then the most compact way is:
对于Lodash 4.x 及更高版本:
_.pickBy({ a: null, b: 1, c: undefined }, _.identity);
>> Object {b: 1}
对于 legacy Lodash 3.x:
For legacy Lodash 3.x:
_.pick(obj, _.identity);
_.pick({ a: null, b: 1, c: undefined }, _.identity);
>> Object {b: 1}
这篇关于如何使用 lodash 从对象中删除未定义和空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文