diff --git a/README.md b/README.md index 776caf373..eb58ef8a6 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,6 @@ average(1, 2, 3); * [`minN`](#minn) * [`nthElement`](#nthelement) * [`partition`](#partition) -* [`pick`](#pick) * [`pull`](#pull) * [`pullAtIndex`](#pullatindex) * [`pullAtValue`](#pullatvalue) @@ -284,7 +283,9 @@ average(1, 2, 3); * [`merge`](#merge) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) +* [`omit`](#omit) * [`orderBy`](#orderby) +* [`pick`](#pick) * [`shallowClone`](#shallowclone) * [`size`](#size) * [`transform`](#transform) @@ -1349,29 +1350,6 @@ partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active':
[⬆ Back to top](#table-of-contents) -### pick - -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. - -```js -const pick = (obj, arr) => - arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); -``` - -
-Examples - -```js -pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 } -``` - -
- -
[⬆ Back to top](#table-of-contents) - - ### pull Mutates the original array to filter out the values specified. @@ -4496,6 +4474,32 @@ objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]
[⬆ Back to top](#table-of-contents) +### 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), {}); +``` + +
+Examples + +```js +omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### orderBy Returns a sorted array of objects ordered by properties and orders. @@ -4530,6 +4534,29 @@ orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', a
[⬆ Back to top](#table-of-contents) +### pick + +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 pairs if the key exists in the object. + +```js +const pick = (obj, arr) => + arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); +``` + +
+Examples + +```js +pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### shallowClone Creates a shallow clone of an object. diff --git a/docs/index.html b/docs/index.html index 5be83bae2..d173421fa 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -231,9 +231,6 @@ Object.assig
   );
 
const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
 partition(users, o => o.active); // [[{ 'user': 'fred',    'age': 40, 'active': true }],[{ 'user': 'barney',  'age': 36, 'active': false }]]
-

pick

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.

const pick = (obj, arr) =>
-  arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
-
pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
 

pull

Mutates the original array to filter out the values specified.

Use Array.filter() and Array.includes() to pull out the values that are not needed. Use Array.length = 0 to mutate the passed in an array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.

(For a snippet that does not mutate the original array see without)

const pull = (arr, ...args) => {
   let argState = Array.isArray(args[0]) ? args[0] : args;
   let pulled = arr.filter((v, i) => !argState.includes(v));
@@ -1008,6 +1005,11 @@ Foo.prototypeShow examples
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
 

objectToPairs

Creates an array of key-value pair arrays from an object.

Use Object.keys() and Array.map() to iterate over the object's keys and produce an array with key-value pairs.

const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
 
objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]
+

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.

const omit = (obj, arr) =>
+  Object.keys(obj)
+    .filter(k => !arr.includes(k))
+    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
+
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }
 

orderBy

Returns a sorted array of objects ordered by properties and orders.

Uses Array.sort(), Array.reduce() on the props array with a default value of 0, use array destructuring to swap the properties position depending on the order passed. If no orders array is passed it sort by 'asc' by default.

const orderBy = (arr, props, orders) =>
   [...arr].sort((a, b) =>
     props.reduce((acc, prop, i) => {
@@ -1021,6 +1023,9 @@ Foo.prototypeShow examples
const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];
 orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
 orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
+

pick

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 pairs if the key exists in the object.

const pick = (obj, arr) =>
+  arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
+
pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
 

shallowClone

Creates a shallow clone of an object.

Use Object.assign() and an empty object ({}) to create a shallow clone of the original.

const shallowClone = obj => Object.assign({}, obj);
 
const a = { x: true, y: 1 };
 const b = shallowClone(a); // a !== b