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

前端 未结 23 2332
既然无缘
既然无缘 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条回答
  •  萌比男神i
    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

提交回复
热议问题