conditional get keys from object javascript code example
Example 1: conditionally add key to object javascript
const condition = getCondition();
const additionalCondition = getAdditionalCondition();
const person = {
firstName: "Max",
...(condition && { lastName: "" }),
...(additionalCondition && { addition: "" }),
};
Example 2: conditionally add property to object ts
const a = {
...(someCondition && {b: 5})
}