Compact and join array

JavaScript, Array · Apr 8, 2022

Removes falsy values from an array and combines the remaining values into a string.

const compactJoin = (arr, delim = ',') => arr.filter(Boolean).join(delim);
compactJoin(['a', '', 'b', 'c']); // 'a,b,c'

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this

  • Compact array

    Removes falsy values from an array.

    JavaScript, Array · Oct 22, 2020

  • JSON to CSV

    Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.

    JavaScript, Array · Oct 13, 2021

  • Tip: Min and max value in a JavaScript array

    When working with numeric arrays in JavaScript, you might need to find the minimum or maximum value. Here's a quick and easy way to do it.

    JavaScript, Array · Nov 6, 2021