There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript:
const obj = {
requiredKey1: ...,
requiredKey2: ...
};
if (someCondition) {
obj.optionalKey1 = ...;
}
Is there a way to define the object all at once with both optional and required keys?
You can use object spread to have an optional property:
let flag1 = true;
let flag2 = false;
// extra cases added by Abdull
let optionalKey8 = 8;
let optionalKey9 = undefined;
let optionalKey10 = false;
let optionalKey11 = null;
let optionalKey12 = "twelve";
const obj = {
requiredKey1: 1,
requiredKey2: 2,
...(flag1 && { optionalKey3: 3 }),
...(flag2 && { optionalKey4: 4, optionalKey5: 5 }), // ignored
...(flag1 && { optionalKey6: 6, optionalKey7: 7 }),
...(optionalKey8 && { optionalKey8 }),
...(optionalKey9 && { optionalKey9 }), // ignored
...(optionalKey10 && { optionalKey10 }), // ignored
...(optionalKey11 && { optionalKey11 }), // ignored
...(optionalKey12 && { optionalKey12 })
};
console.log(obj);