diff --git a/README.md b/README.md index c19623506..08fbe0fcd 100644 --- a/README.md +++ b/README.md @@ -4054,13 +4054,13 @@ orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', a ### select -Retrieve a property indicated by the selector from an object. +Retrieve a set of properties indicated by the given selectors from an object. -If the property does not exists returns `undefined`. +Use `Array.map()` for each selector, `String.split('.')` to split each selector and `Array.reduce()` to get the value indicated by it. ```js -const select = (from, selector) => - selector.split('.').reduce((prev, cur) => prev && prev[cur], from); +const select = (from, ...selectors) => + [...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from)); ```
@@ -4068,7 +4068,8 @@ const select = (from, selector) => ```js const obj = { selector: { to: { val: 'val to select' } } }; -select(obj, 'selector.to.val'); // 'val to select' +select(obj, 'selector.to.val'); // ['val to select'] +select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }] ```
@@ -5364,6 +5365,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index c99bf18b3..2979291a1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -879,10 +879,11 @@ console.log<
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}]
-

select

Retrieve a property indicated by the selector from an object.

If the property does not exists returns undefined.

const select = (from, selector) =>
-  selector.split('.').reduce((prev, cur) => prev && prev[cur], from);
+

select

Retrieve a set of properties indicated by the given selectors from an object.

Use Array.map() for each selector, String.split('.') to split each selector and Array.reduce() to get the value indicated by it.

const select = (from, ...selectors) =>
+  [...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from));
 
const obj = { selector: { to: { val: 'val to select' } } };
-select(obj, 'selector.to.val'); // 'val to select'
+select(obj, 'selector.to.val'); // ['val to select']
+select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }]
 

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
@@ -1198,6 +1199,7 @@ Logs: {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index 21960086e..00f7933f0 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -34,6 +34,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,