Monday, March 2, 2020

js conditionally include or exclude an element in a static array declaration

via this stackoverflow, you can use the spread operator and the ternary operator to conditionally include or exclude something in an array declaration ala:

const myArray = [
  'always this thing',
  ...(testFuntion() ? ['sometimes this thing'] : []),
  'and always this thing',
];


No comments:

Post a Comment