Prepare repository for merge
This commit is contained in:
23
javascript/snippets/omit.md
Normal file
23
javascript/snippets/omit.md
Normal file
@ -0,0 +1,23 @@
|
||||
---
|
||||
title: Omit object keys
|
||||
type: snippet
|
||||
tags: [object]
|
||||
cover: broken-screen
|
||||
dateModified: 2020-10-21T21:54:53+03:00
|
||||
---
|
||||
|
||||
Omits the key-value pairs corresponding to the given keys from an object.
|
||||
|
||||
- Use `Object.keys()`, `Array.prototype.filter()` and `Array.prototype.includes()` to remove the provided keys.
|
||||
- Use `Array.prototype.reduce()` to convert the filtered keys back to an object with the corresponding key-value pairs.
|
||||
|
||||
```js
|
||||
const omit = (obj, arr) =>
|
||||
Object.keys(obj)
|
||||
.filter(k => !arr.includes(k))
|
||||
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
|
||||
```
|
||||
|
||||
```js
|
||||
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }
|
||||
```
|
||||
Reference in New Issue
Block a user