-1

I am using library Lodash and I need from my javascript object remove just possible undefined properties, but I want keep null properties.

for example if I would have object like this:

var fooObject = {propA:undefined, propB:null, propC:'fooo'};

I expect output like this:

{propB:null, propC:'fooo'}

I tried this:

.pickBy(fooObject, _.identity);

but it remove also null values. Does Lodash contain some function for that? Thanks.

Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174

4 Answers4

1

Return anything that is NOT _.isUndefined :

_.pickBy({propA:undefined, propB:null, propC:'fooo'}, function(val){
     return !_.isUndefined(val);
});

Or even more nicely :

_.omitBy({propA:undefined, propB:null, propC:'fooo'}, _.isUndefined);
yvoytovych
  • 871
  • 4
  • 12
1

Try with:

_.pickBy(fooObject, v => v !== undefined)
hsz
  • 148,279
  • 62
  • 259
  • 315
0

Try using .omitBy method .

For undefined and null values.

_.isUndefined and _.isNull

 var fooObject = {propA:undefined, propB:null, propC:'fooo'};

To remove undefined values from object

var newObj= _(fooObject).omitBy(_.isUndefined).value();
 console.log(newObj);

Incase,If you want to remove both undefined and null

var result = _(fooObject).omitBy(_.isUndefined).omitBy(_.isNull).value();
console.log(result);

Hope this helps..!

-1

//simple javascript

var fooObject = {propA:undefined, propB:null, propC:'fooo'};

for(var prop in fooObject)
{
if(fooObject[prop]===undefined)
delete fooObject[prop];
}
console.log(fooObject);
John Willson
  • 444
  • 1
  • 3
  • 13