Add omit
This commit is contained in:
17
snippets/omit.md
Normal file
17
snippets/omit.md
Normal file
@ -0,0 +1,17 @@
|
||||
### omit
|
||||
|
||||
Omits the key-value pairs corresponding to the given keys from an object.
|
||||
|
||||
Use `Object.keys(obj)`, `Array.filter()` and `Array.includes()` to remove the provided keys.
|
||||
Use `Array.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 }
|
||||
```
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Picks the key-value pairs corresponding to the given keys from an object.
|
||||
|
||||
Use `Array.reduce()` to convert the filtered/picked keys back to an object with the corresponding key-value pair if the key exists in the obj.
|
||||
Use `Array.reduce()` to convert the filtered/picked keys back to an object with the corresponding key-value pairs if the key exists in the object.
|
||||
|
||||
```js
|
||||
const pick = (obj, arr) =>
|
||||
|
||||
Reference in New Issue
Block a user