Removes falsy values from an array and combines the remaining values into a string.
Array.prototype.filter()
to filter out falsy values (false
, null
, 0
, ""
, undefined
, and NaN
).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'
Would you like to help us improve 30 seconds of code?Take a quick survey
JavaScript, Array
Removes falsy values from an array.
JavaScript, Array
Converts an array of objects to a comma-separated values (CSV) string that contains only the columns
specified.
JavaScript, Array
Mutates the original array to filter out the values at the specified indexes. Returns the removed elements.