Simplify deepClone snippet's handling of arrays.

This commit is contained in:
Oscar
2018-05-10 19:22:35 +01:00
parent f2abd5c226
commit 2930713371
2 changed files with 4 additions and 18 deletions

View File

@ -1,16 +1,8 @@
const deepClone = obj => {
if (Array.isArray(obj)){
let arr = [];
obj.forEach(
(i,v) => (arr[i] = typeof v === 'object' ? deepClone(v) : v)
)
return arr;
}else {
let clone = Object.assign({}, obj);
Object.keys(clone).forEach(
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
);
return clone;
}
return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj;
};
module.exports = deepClone;