Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js add to array conditionally

const cond = false;
const arr = [
  ...(cond ? ['a'] : []),
  'b',
];
// result: 
//if cond true: ['a','b']
//if cond false: ['b']

// from https://2ality.com/2017/04/conditional-literal-entries.html
 
PREVIOUS NEXT
Tagged: #js #add #array #conditionally
ADD COMMENT
Topic
Name
1+3 =