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) =>
|
||||
|
||||
@ -131,6 +131,7 @@ objectFromPairs:object,array
|
||||
objectToPairs:object,array
|
||||
observeMutations:browser,event,advanced
|
||||
off:browser,event
|
||||
omit:object,array
|
||||
on:browser,event
|
||||
once:function
|
||||
onUserInputChange:browser,event,advanced
|
||||
@ -139,7 +140,7 @@ palindrome:string
|
||||
parseCookie:utility,string
|
||||
partition:array,object,function
|
||||
percentile:math
|
||||
pick:array
|
||||
pick:object,array
|
||||
pipeFunctions:adapter,function
|
||||
pluralize:string
|
||||
powerset:math
|
||||
|
||||
Reference in New Issue
Block a user