Files
30-seconds-of-code/snippets/compactJoin.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

539 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
Compact and join array array chalarangelo racoon 2022-04-08T05:00:00-04:00

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, and NaN).
  • 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'