Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

Conditionally add elements to an array

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

>> ['b']
Comment

PREVIOUS NEXT
Code Example
Javascript :: open modal js 
Javascript :: add comma after every 3 digits javascript 
Javascript :: find space in string js 
Javascript :: enter only numbers in input field angular 
Javascript :: link button material ui 
Javascript :: react not getting img by src 
Javascript :: json.stringify formatting 
Javascript :: i18n react meta description 
Javascript :: reading files with node.js 
Javascript :: email id domain check javascript 
Javascript :: javascript regex check phone number 
Javascript :: check if a column is unique sql 
Javascript :: node read file sync 
Javascript :: get date javascript 
Javascript :: https://mongoosejs.com/docs/deprecations.html#findandmodify 
Javascript :: js convert array of array to array 
Javascript :: how can i validate a password without regex in js 
Javascript :: remove duplicates in json array based on two fields in lodash 
Javascript :: Remove duplication from array in javascript 
Javascript :: react toastify does not have design 
Javascript :: change the mouse pointer javascript 
Javascript :: export default new class in es6 
Javascript :: javascript calculate 24 hours ago 
Javascript :: text inside an image component react native 
Javascript :: MVC view pass model to javascript function 
Javascript :: append element in a div as first child 
Javascript :: calculus of finite differences calculator 
Javascript :: react history.push 
Javascript :: nodejs catch uncaught exception 
Javascript :: Javascript get sum of array values 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =