123456789101112131415161718192021222324252627282930313233 |
- export function deepClone (oldData, newData) {
- if (newData instanceof Array) {
- if (oldData.length !== newData.length) {
- oldData = new Array(newData.length)
- }
- for (var i = 0; i < newData.length; i++) {
- if (newData[i] instanceof Array) {
- oldData[i] = new Array(newData[i].length)
- deepClone(oldData[i], newData[i])
- } else if (newData[i] instanceof Object) {
- oldData[i] = {}
- deepClone(oldData[i], newData[i])
- } else {
- oldData[i] = newData[i]
- }
- }
- } else if (newData instanceof Object) {
- if (oldData.length > 0) {
- oldData = {}
- }
- for (var key in newData) {
- if (newData[key] instanceof Array) {
- oldData[key] = new Array(newData[key].length)
- deepClone(oldData[key], newData[key])
- } else if (newData[key] instanceof Object) {
- oldData[key] = {}
- deepClone(oldData[key], newData[key])
- } else {
- oldData[key] = newData[key]
- }
- }
- }
- }
|