Rename js snippets
This commit is contained in:
21
snippets/js/s/compact-array.md
Normal file
21
snippets/js/s/compact-array.md
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
title: Compact array
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [array]
|
||||
cover: basket-paper
|
||||
dateModified: 2020-10-22T20:23:47+03:00
|
||||
---
|
||||
|
||||
Removes falsy values from an array.
|
||||
|
||||
- Use `Array.prototype.filter()` to filter out falsy values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||
|
||||
```js
|
||||
const compact = arr => arr.filter(Boolean);
|
||||
```
|
||||
|
||||
```js
|
||||
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);
|
||||
// [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
Reference in New Issue
Block a user