Compact and join array
JavaScript, Array · Apr 8, 2022

Removes falsy values from an array and combines the remaining values into a string.
- Use
Array.prototype.filter()
to filter out falsy values (false
,null
,0
,""
,undefined
, andNaN
). - Use
Array.prototype.join()
to join the remaining values into a string.
const compactJoin = (arr, delim = ',') => arr.filter(Boolean).join(delim);
compactJoin(['a', '', 'b', 'c']); // 'a,b,c'