const includeSalary = true;
const employee = {
id: 1,
name: 'Ofir',
...(includeSalary && { salary: 1000 }),
}
const a = {
...(someCondition && {b: 5})
}
// Add Propperties to an object conditionally.
const isOnline = true;
const user = {
id: 1,
name: 'John',
...(isOnline && { active: true }),
}
console.log(user);
// { id: 1, name: 'John', active: true }
const trueCondition = true;const falseCondition = false;const obj = { ...(trueCondition && { dogs: "woof" }), ...(falseCondition && { cats: "meow" }),};// { dogs: 'woof' }
// JS object add attribute conditionally
const isActive = true;
const obj = {
id: 1,
...(isActive && { active: true }),
}