Warunkowo dodaj właściwość do obiektu
const includeSalary = true;
const employee = {
id: 1,
name: 'Ofir',
...(includeSalary && { salary: 1000 }),
}
Ofir Salem
const includeSalary = true;
const employee = {
id: 1,
name: 'Ofir',
...(includeSalary && { salary: 1000 }),
}
// 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' }