Prepare repository for merge
This commit is contained in:
21
javascript/snippets/compact-join.md
Normal file
21
javascript/snippets/compact-join.md
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
title: Compact and join array
|
||||
type: snippet
|
||||
tags: [array]
|
||||
author: chalarangelo
|
||||
cover: racoon
|
||||
dateModified: 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.
|
||||
|
||||
```js
|
||||
const compactJoin = (arr, delim = ',') => arr.filter(Boolean).join(delim);
|
||||
```
|
||||
|
||||
```js
|
||||
compactJoin(['a', '', 'b', 'c']); // 'a,b,c'
|
||||
```
|
||||
Reference in New Issue
Block a user