ES6 Code:
Object.assign(a, conditionB ? { b: 1 } : null,
conditionC ? { c: 2 } : null,
conditionD ? { d: 3 } : null);
Sugar ES6 Code:
const a = {
...(someCondition && {b: 5})
}
Above works as : spread operator is like a shorthand of Object.assign and have lower precedence than the && operator. It ignore value without property (boolean, null, undefined, number), and add all properties of the object after the … in place. remember the && operator return the right value if true, or false otherwise. So if someCondition is true, {b : 5} will be passed to the … operator, resulting in adding the property b to a with value 5. If someCondition is false, false will be passed to the … operator. resulting in nothing added.