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

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

    To complete the other answers, in lodash 4 to ignore only undefined and null (And not properties like false) you can use a predicate in _.pickBy:

    _.pickBy(obj, v !== null && v !== undefined)

    Example below :

    const obj = { a: undefined, b: 123, c: true, d: false, e: null};
    
    const filteredObject = _.pickBy(obj, v => v !== null && v !== undefined);
    
    console.log = (obj) => document.write(JSON.stringify(filteredObject, null, 2));
    console.log(filteredObject);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>

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

    Taking in account that undefined == null we can write as follows:

    let collection = {
      a: undefined,
      b: 2,
      c: 4,
      d: null,
    }
    
    console.log(_.omit(collection, it => it == null))
    // -> { b: 2, c: 4 }
    

    JSBin example

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

    I like using _.pickBy, because you have full control over what you are removing:

    var person = {"name":"bill","age":21,"sex":undefined,"height":null};
    
    var cleanPerson = _.pickBy(person, function(value, key) {
      return !(value === undefined || value === null);
    });
    

    Source: https://www.codegrepper.com/?search_term=lodash+remove+undefined+values+from+object

    0 讨论(0)
  • 2020-11-29 18:29
    var my_object = { a:undefined, b:2, c:4, d:undefined };
    
    var newObject = _.reject(my_collection, function(val){ return _.isUndefined(val) })
    
    //--> newCollection = { b: 2, c: 4 }
    
    0 讨论(0)
  • 2020-11-29 18:29

    For deep nested object and arrays. and exclude empty values from string and NaN

    function isBlank(value) {
      return _.isEmpty(value) && !_.isNumber(value) || _.isNaN(value);
    }
    var removeObjectsWithNull = (obj) => {
      return _(obj).pickBy(_.isObject)
        .mapValues(removeObjectsWithNull)
        .assign(_.omitBy(obj, _.isObject))
        .assign(_.omitBy(obj, _.isArray))
        .omitBy(_.isNil).omitBy(isBlank)
        .value();
    }
    var obj = {
      teste: undefined,
      nullV: null,
      x: 10,
      name: 'Maria Sophia Moura',
      a: null,
      b: '',
      c: {
        a: [{
          n: 'Gleidson',
          i: 248
        }, {
          t: 'Marta'
        }],
        g: 'Teste',
        eager: {
          p: 'Palavra'
        }
      }
    }
    removeObjectsWithNull(obj)
    

    result:

    {
       "c": {
          "a": [
             {
                "n": "Gleidson",
                "i": 248
             },
             {
                "t": "Marta"
             }
          ],
          "g": "Teste",
          "eager": {
             "p": "Palavra"
          }
       },
       "x": 10,
       "name": "Maria Sophia Moura"
    }
    
    0 讨论(0)
  • 2020-11-29 18:29

    For those of you getting here looking to remove from an array of objects and using lodash you can do something like this:

    
     const objects = [{ a: 'string', b: false, c: 'string', d: undefined }]
     const result = objects.map(({ a, b, c, d }) => _.pickBy({ a,b,c,d }, _.identity))
    
     // [{ a: 'string', c: 'string' }]
    
    

    Note: You don't have to destruct if you don't want to.

    0 讨论(0)
提交回复
热议问题