Add property to object only if value is defined

Is there a better way to add a property to an object only if the value is defined :

const destination = {};
const value = getValue();

if (_(value).isObject()) { // In this case, I only want an object
    destination.value = value;
}

In this case, the destination.value property exists only if value has a value. Is there a better way to do this ?

Upvotes: 34

Views: 54181

Answers (6)

Taiwei Tuan
Taiwei Tuan

Reputation: 1122

A similar solution to the top-voted answer but slightly shorter --

const value = getValue()
const destination = {
    ...(value && { value }),
}

This will be valuable if you are conditionally overwriting many properties. Just now I had to do --

const destination = {
    ...(newAddress.givenName && { firstName: newAddress.givenName }),
    ...(newAddress.familyName && { lastName: newAddress.familyName }),
    ...(newAddress.organizationName && { company: newAddress.organizationName }),
    ...(newAddress.phone && { phoneNumber: newAddress.phone }),
    ...(newAddress.streetAddress && { address1: newAddress.streetAddress }),
    ...(newAddress.streetAddress2 && { address2: newAddress.streetAddress2 }),
    ...(newAddress.city && { city: newAddress.city }),
    ...(newAddress.region && { state: newAddress.region }),
    ...(newAddress.country && { country: newAddress.country }),
    ...(newAddress.postalCode && { zip: newAddress.postalCode }),
}

const result = {
    ...defaultAddress,
    ...destinationAddress
}

Where any of the newAddress can be undefined or null, making it extra safe.

Upvotes: 5

Ravindra Thorat
Ravindra Thorat

Reputation: 2002

Update

Here is a better ES6 way,

const destination = {};
const value = getValue();
destination = {
    ...destination, 
    ...(value && { value }),
}

Here the assumption is you want to make a key with the name value itself.

Another approach

just do like this,

const destination = {};
const value = getValue();

value && (destination.value = value); // here is the one liner way

This will make sure that the value attribute is getting create only if value is defined

Upvotes: 41

Henrique Limas
Henrique Limas

Reputation: 307

An alternative way could be to put values that you want to add in an array and use filter and forEach (or reduce) to add it to the destination object.

const destination = {}
const propsToAdd = [{ key: 'value', value: getValue() }]

propsToAdd
 .filter(data => _.isObject(data.value))
 .forEach(data => destination[data.key] = data.value);

That way you can also add a function to the object that you can use in the filter to validate other types rather than only objects.

Upvotes: 0

tmikeschu
tmikeschu

Reputation: 1418

With ES6, I like to do conditional spreading:

const value = getValue()
const destination = {
  ...(value ? { value } : {})
}

And you can do whatever boolean check you need in the ternary. In your case, checking to see if value is an object.

Upvotes: 91

trincot
trincot

Reputation: 350300

You could also do the Object-test with Object(value) === value and perform the conditional assignment with Object.assign:

Object.assign(destination, Object(value) === value && { value });

Whether this is "better" is debatable. Pick what you like most and best suits your expectations.

Upvotes: 5

Gratus D.
Gratus D.

Reputation: 877

if(typeof value != 'undefined')

Unless you want to exclude null also (null is an object in javascript). In that case:

if(typeof value != 'undefined' && value !=null) 

Upvotes: 1

Related Questions