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

前端 未结 23 2224
既然无缘
既然无缘 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:18

    I encountered a similar problem with removing undefined from an object (deeply), and found that if you are OK to convert your plain old object and use JSON, a quick and dirty helper function would look like this:

    function stripUndefined(obj) {
      return JSON.parse(JSON.stringify(obj));
    }
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description

    "...If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array)."

    0 讨论(0)
  • 2020-11-29 18:19

    For deep nested object you can use my snippet for lodash > 4

    const removeObjectsWithNull = (obj) => {
        return _(obj)
          .pickBy(_.isObject) // get only objects
          .mapValues(removeObjectsWithNull) // call only for values as objects
          .assign(_.omitBy(obj, _.isObject)) // save back result that is not object
          .omitBy(_.isNil) // remove null and undefined from object
          .value(); // get value
    };
    
    0 讨论(0)
  • 2020-11-29 18:21

    According to lodash docs:

    _.compact(_.map(array, fn))
    

    Also you can filter out all nulls

    0 讨论(0)
  • 2020-11-29 18:23

    The correct answer is:

    _.omitBy({ a: null, b: 1, c: undefined, d: false }, _.isNil)
    

    That results in:

    {b: 1, d: false}
    

    The alternative given here by other people:

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

    Will remove also false values which is not desired here.

    0 讨论(0)
  • 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}
    
    0 讨论(0)
  • 2020-11-29 18:25

    You can simply chain _.omit() with _.isUndefined and _.isNull compositions, and get the result with lazy evaluation.

    Demo

    var result = _(my_object).omit(_.isUndefined).omit(_.isNull).value();
    

    Update March 14, 2016:

    As mentioned by dylants in the comment section, you should use the _.omitBy() function since it uses a predicate instead of a property. You should use this for lodash version 4.0.0 and above.

    DEMO

    var result = _(my_object).omitBy(_.isUndefined).omitBy(_.isNull).value();
    

    Update June 1, 2016:

    As commented by Max Truxa, lodash already provided an alternative _.isNil, which checks for both null and undefined:

    var result = _.omitBy(my_object, _.isNil);
    
    0 讨论(0)
提交回复
热议问题