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

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

提交回复
热议问题