Merge remote-tracking branch 'origin/master'
This commit is contained in:
32
README.md
32
README.md
@ -272,6 +272,7 @@ average(1, 2, 3);
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
|
||||
* [`deepClone`](#deepclone)
|
||||
* [`defaults`](#defaults)
|
||||
* [`equals`](#equals-)
|
||||
* [`findKey`](#findkey)
|
||||
@ -4191,6 +4192,37 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
|
||||
---
|
||||
## 🗃️ 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
|
||||
|
||||
Assigns default values for all properties in an object that are `undefined`.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -10,10 +10,7 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee
|
||||
const deepClone = obj => {
|
||||
let clone = Object.assign({}, obj);
|
||||
Object.keys(clone).forEach(
|
||||
key =>
|
||||
(clone[key] = typeof obj[key] === 'object'
|
||||
? deepClone(obj[key])
|
||||
: obj[key])
|
||||
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
||||
);
|
||||
return clone;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user