parent
52cdc02a32
commit
282de476da
1 changed files with 26 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||||||
|
export const deepCopy = function(obj) { |
||||||
|
if (obj == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (typeof obj !== "object") return obj; |
||||||
|
let result; |
||||||
|
if (Array.isArray(obj)) { |
||||||
|
result = []; |
||||||
|
obj.forEach(item => { |
||||||
|
result.push( |
||||||
|
typeof item === "object" && !(item instanceof Date) |
||||||
|
? deepCopy(item) |
||||||
|
: item |
||||||
|
); |
||||||
|
}); |
||||||
|
} else { |
||||||
|
result = {}; |
||||||
|
Object.keys(obj).forEach(key => { |
||||||
|
result[key] = |
||||||
|
typeof obj[key] === "object" && !(obj[key] instanceof Date) |
||||||
|
? deepCopy(obj[key]) |
||||||
|
: obj[key]; |
||||||
|
}); |
||||||
|
} |
||||||
|
return result; |
||||||
|
}; |
Loading…
Reference in new issue