deepClone: Fixed indents & failing tests

This commit is contained in:
Oscar
2018-05-11 10:23:59 +01:00
parent 76c56195b1
commit 1f7377e7d0
2 changed files with 9 additions and 7 deletions

View File

@ -8,12 +8,13 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee
```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 Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj;
let clone = Object.assign({}, obj);
Object.keys(clone).forEach(
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
);
return Array.isArray(obj) ? Array.from(clone) : clone;
};
```
@ -21,6 +22,7 @@ const deepClone = obj => {
const a = { foo: 'bar', obj: { a: 1, b: 2 } };
const b = deepClone(a); // a !== b, a.obj !== b.obj
```