deep-clone.js 392 B

12345678910111213141516171819202122
  1. import { isDef } from './index';
  2. export function deepClone(obj) {
  3. if (!isDef(obj)) {
  4. return obj;
  5. }
  6. if (Array.isArray(obj)) {
  7. return obj.map(function (item) {
  8. return deepClone(item);
  9. });
  10. }
  11. if (typeof obj === 'object') {
  12. var to = {};
  13. Object.keys(obj).forEach(function (key) {
  14. to[key] = deepClone(obj[key]);
  15. });
  16. return to;
  17. }
  18. return obj;
  19. }