insert field in an object js code example
Example 1: insert condition in a object javascript
const arr = [
...(isConditionTrue() ? [{
key: 'value'
}] : [])
];
const obj = {
...(isConditionTrue() ? {key: 'value'} : {})
};
Example 2: insert condition in a object javascript
const a = {
...(someCondition && {b: 5})
}