Conditionally adding keys to JavaScript objects using spread operators and short-circuit evaluation
const buildAnObjectFromAQuery = (query) => { const object = {}; if (query.foo) { object.foo = query.foo; } if (query.bar) { object.bar = query.bar; } return object; }
equivalent to
const buildAnObjectFromAQuery = query => ({ ...query.foo && { foo: query.foo }, ...query.bar && { bar: query.bar }, });
as array
...(repeatOn.length > 0) && { repeatOn },