How to remove undefined and null values from an object using lodash?

前端 未结 23 2342
既然无缘
既然无缘 2020-11-29 18:06

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

23条回答
  •  孤独总比滥情好
    2020-11-29 18:24

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

    For Lodash 4.x and later:

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

    For legacy Lodash 3.x:

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

提交回复
热议问题