datas.js 995 B

123456789101112131415161718192021222324252627282930313233
  1. export function deepClone (oldData, newData) {
  2. if (newData instanceof Array) {
  3. if (oldData.length !== newData.length) {
  4. oldData = new Array(newData.length)
  5. }
  6. for (var i = 0; i < newData.length; i++) {
  7. if (newData[i] instanceof Array) {
  8. oldData[i] = new Array(newData[i].length)
  9. deepClone(oldData[i], newData[i])
  10. } else if (newData[i] instanceof Object) {
  11. oldData[i] = {}
  12. deepClone(oldData[i], newData[i])
  13. } else {
  14. oldData[i] = newData[i]
  15. }
  16. }
  17. } else if (newData instanceof Object) {
  18. if (oldData.length > 0) {
  19. oldData = {}
  20. }
  21. for (var key in newData) {
  22. if (newData[key] instanceof Array) {
  23. oldData[key] = new Array(newData[key].length)
  24. deepClone(oldData[key], newData[key])
  25. } else if (newData[key] instanceof Object) {
  26. oldData[key] = {}
  27. deepClone(oldData[key], newData[key])
  28. } else {
  29. oldData[key] = newData[key]
  30. }
  31. }
  32. }
  33. }