如何使用lodash从对象中删除undefined和null值? [英] How to remove undefined and null values from an object using lodash?

查看:5402
本文介绍了如何使用lodash从对象中删除undefined和null值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

如果你想删除所有 falsey 值然后最紧凑的方式是:

If you want to remove all falsey values then the most compact way is:

_.pick(obj, _.identity);

例如(Lodash 3.x):

For example (Lodash 3.x):

_.pick({ a: null, b: 1, c: undefined }, _.identity);
>> Object {b: 1}

对于Lodash 4.x:

For Lodash 4.x:

_.pickBy({ a: null, b: 1, c: undefined }, _.identity);
>> Object {b: 1}

这篇关于如何使用lodash从对象中删除undefined和null值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆