Is there a way to create an object in JavaScript only if certain keys are not undefined?
For example, suppose I have some network request that requires a payload:
const payload = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
};
Now, let's suppose that key2
and key3
are optional parameters to a function. If I do not pass those in, and they are left as undefined
will the object become:
const payload = {
key1: 'value1',
};
I'm thinking that maybe using Object.create(...) can help me with this?
Any thoughts? Thanks