Conditionally add properties inside Object in Javascript
const cond = false;
const obj = {
...(cond ? {a: 1} : {}),
b: 2,
};
// {b: 2}
const cond = false;
const obj = {
...(cond ? {a: 1} : {}),
b: 2,
};
// {b: 2}
Article 2024-11-04
In PHP, using the & (ampersand) symbol before a variable…