Travis build: 1581
This commit is contained in:
73
README.md
73
README.md
@ -319,6 +319,7 @@ average(1, 2, 3);
|
|||||||
* [`equals`](#equals-)
|
* [`equals`](#equals-)
|
||||||
* [`findKey`](#findkey)
|
* [`findKey`](#findkey)
|
||||||
* [`findLastKey`](#findlastkey)
|
* [`findLastKey`](#findlastkey)
|
||||||
|
* [`flattenObject`](#flattenobject)
|
||||||
* [`forOwn`](#forown)
|
* [`forOwn`](#forown)
|
||||||
* [`forOwnRight`](#forownright)
|
* [`forOwnRight`](#forownright)
|
||||||
* [`functions`](#functions)
|
* [`functions`](#functions)
|
||||||
@ -341,6 +342,7 @@ average(1, 2, 3);
|
|||||||
* [`size`](#size)
|
* [`size`](#size)
|
||||||
* [`transform`](#transform)
|
* [`transform`](#transform)
|
||||||
* [`truthCheckCollection`](#truthcheckcollection)
|
* [`truthCheckCollection`](#truthcheckcollection)
|
||||||
|
* [`unflattenObject`](#unflattenobject-)
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@ -5541,6 +5543,38 @@ findLastKey(
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### flattenObject
|
||||||
|
|
||||||
|
Flatten an object with the paths for keys.
|
||||||
|
|
||||||
|
Use recursion.
|
||||||
|
Use `Object.keys(obj)` combined with `Array.reduce()` to convert every leaf node to a flattened path node.
|
||||||
|
If the value of a key is an object, the function calls itself with the appropriate `prefix` to create the path using `Object.assign()`.
|
||||||
|
Otherwise, it adds the appropriate prefixed key-value pair to the accumulator object.
|
||||||
|
You should always omit the second argument, `prefix`, unless you want every key to have a prefix.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const flattenObject = (obj, prefix = '') =>
|
||||||
|
Object.keys(obj).reduce((acc, k) => {
|
||||||
|
const pre = prefix.length ? prefix + '.' : '';
|
||||||
|
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
|
||||||
|
else acc[pre + k] = obj[k];
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```js
|
||||||
|
flattenObject({ a: { b: { c: 1 } }, d: 1 }); // { 'a.b.c': 1, d: 1 }
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### forOwn
|
### forOwn
|
||||||
|
|
||||||
Iterates over all own properties of an object, running a callback for each one.
|
Iterates over all own properties of an object, running a callback for each one.
|
||||||
@ -6153,6 +6187,45 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
|
|||||||
|
|
||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### unflattenObject 
|
||||||
|
|
||||||
|
Unlatten an object with the paths for keys.
|
||||||
|
|
||||||
|
Use `Object.keys(obj)` combined with `Array.reduce()` to convert flattened path node to a leaf node.
|
||||||
|
If the value of a key contains a dot delimiter (`.`), use `Array.split('.')`, string transformations and `JSON.parse()` to create an object, then `Object.assign()` to create the leaf node.
|
||||||
|
Otherwise, add the appropriate key-value pair to the accumulator object.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const unflattenObject = obj =>
|
||||||
|
Object.keys(obj).reduce((acc, k) => {
|
||||||
|
if (k.indexOf('.') !== -1) {
|
||||||
|
const keys = k.split('.');
|
||||||
|
Object.assign(
|
||||||
|
acc,
|
||||||
|
JSON.parse(
|
||||||
|
'{' +
|
||||||
|
keys.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`)).join('') +
|
||||||
|
obj[k] +
|
||||||
|
'}'.repeat(keys.length)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else acc[k] = obj[k];
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```js
|
||||||
|
unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
---
|
---
|
||||||
## 📜 String
|
## 📜 String
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -11,12 +11,8 @@ You should always omit the second argument, `prefix`, unless you want every key
|
|||||||
```js
|
```js
|
||||||
const flattenObject = (obj, prefix = '') =>
|
const flattenObject = (obj, prefix = '') =>
|
||||||
Object.keys(obj).reduce((acc, k) => {
|
Object.keys(obj).reduce((acc, k) => {
|
||||||
const pre = prefix.length ? (prefix + '.') : '';
|
const pre = prefix.length ? prefix + '.' : '';
|
||||||
if (typeof obj[k] === 'object')
|
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
|
||||||
Object.assign(
|
|
||||||
acc,
|
|
||||||
flattenObject(obj[k], pre + k)
|
|
||||||
);
|
|
||||||
else acc[pre + k] = obj[k];
|
else acc[pre + k] = obj[k];
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|||||||
@ -15,9 +15,7 @@ const unflattenObject = obj =>
|
|||||||
acc,
|
acc,
|
||||||
JSON.parse(
|
JSON.parse(
|
||||||
'{' +
|
'{' +
|
||||||
keys
|
keys.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`)).join('') +
|
||||||
.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`))
|
|
||||||
.join('') +
|
|
||||||
obj[k] +
|
obj[k] +
|
||||||
'}'.repeat(keys.length)
|
'}'.repeat(keys.length)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user