lodash object remove empty values code example

Example 1: lodash remove undefined values from object

var person = {"name":"bill","age":21,"sex":undefined,"height":"crap"};
//remove undefined properties or other crap
var cleanPerson = _.pickBy(person, function(value, key) {
  return !(value === undefined || value === "crap");
});
//cleanPerson is now { "name": "bill","age": 21}

Example 2: lodash remove null from object

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

Example 3: remove empty object from array lodash

var myArrayFiltered = myArray.filter((ele) => {
  return ele.constructor === Object && Object.keys(ele).length > 0
});