deepClone: Fixed indents & failing tests
This commit is contained in:
@ -8,12 +8,13 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
|
|
||||||
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 => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
||||||
);
|
);
|
||||||
return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj;
|
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 a = { foo: 'bar', obj: { a: 1, b: 2 } };
|
||||||
const b = deepClone(a); // a !== b, a.obj !== b.obj
|
const b = deepClone(a); // a !== b, a.obj !== b.obj
|
||||||
```
|
```
|
||||||
|
|||||||
@ -3,6 +3,6 @@ let clone = Object.assign({}, obj);
|
|||||||
Object.keys(clone).forEach(
|
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 Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj;
|
return Array.isArray(obj) ? Array.from(clone) : clone;
|
||||||
};
|
};
|
||||||
module.exports = deepClone;
|
module.exports = deepClone;
|
||||||
Reference in New Issue
Block a user