how can i define conditional array elements? i want to do something like this:
const cond = true;
const myArr = [\"foo\", cond && \"bar\"];
>
You don't have so many options other than using push
:
const cond = true;
const myArr = ["foo"];
if (cond) myArr.push("bar");
Another idea is potentially adding null's and filtering them out:
const cond = true;
const myArr = ["foo", cond ? "bar" : null];
myArr = myArr.filter((item) => item !== null);