fix(deepClone): Fixed problems with array values

Would previously create an object with indices as keys. Now properly clones as array.

#658
This commit is contained in:
oh
2018-05-09 20:01:55 +01:00
parent 43d1cf6b75
commit f2abd5c226
3 changed files with 27 additions and 5 deletions

View File

@ -1,8 +1,16 @@
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;
}
};
module.exports = deepClone;