Merge remote-tracking branch 'origin/master'

This commit is contained in:
Angelos Chalaris
2018-01-23 20:54:17 +02:00
3 changed files with 44 additions and 6 deletions

View File

@ -272,6 +272,7 @@ average(1, 2, 3);
<details> <details>
<summary>View contents</summary> <summary>View contents</summary>
* [`deepClone`](#deepclone)
* [`defaults`](#defaults) * [`defaults`](#defaults)
* [`equals`](#equals-) * [`equals`](#equals-)
* [`findKey`](#findkey) * [`findKey`](#findkey)
@ -4191,6 +4192,37 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
--- ---
## 🗃️ Object ## 🗃️ Object
### deepClone
Creates a deep clone of an object.
Use recursion.
Use `Object.assign()` and an empty object (`{}`) to create a shallow clone of the original.
Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs need to be deep cloned.
```js
const deepClone = obj => {
let clone = Object.assign({}, obj);
Object.keys(clone).forEach(
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
);
return clone;
};
```
<details>
<summary>Examples</summary>
```js
const a = { foo: 'bar', obj: { a: 1, b: 2 } };
const b = deepClone(a); // a !== b, a.obj !== b.obj
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### defaults ### defaults
Assigns default values for all properties in an object that are `undefined`. Assigns default values for all properties in an object that are `undefined`.

File diff suppressed because one or more lines are too long

View File

@ -10,10 +10,7 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee
const deepClone = obj => { const deepClone = obj => {
let clone = Object.assign({}, obj); let clone = Object.assign({}, obj);
Object.keys(clone).forEach( Object.keys(clone).forEach(
key => key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
(clone[key] = typeof obj[key] === 'object'
? deepClone(obj[key])
: obj[key])
); );
return clone; return clone;
}; };