Giter VIP home page Giter VIP logo

Comments (8)

shiyachuang avatar shiyachuang commented on August 23, 2024
const deepClone = (data) => {
   var obj;
   if (isArray(data)) {
      obj = [];
   } else if (isObject(data)) {
      obj = {};
   } else {
      //不再具有下一层次
      return data;
   }
   if (isArray(data)) {
      for (var i = 0, len = data.length; i < len; i++) {
         obj.push(deepClone(data[i]));
      }
   } else if (isObject(data)) {
      for (var key in data) {
         obj[key] = deepClone(data[key]);
      }
   }
   return obj;
};

> 

from frontend-interview.

bc-baicha avatar bc-baicha commented on August 23, 2024

function demo(obj) {
if (typeof obj !== 'object') return obj;
let result = obj instanceof Array ? [] : {};
for (const key in obj) {
result[key] = typeof obj[key] === 'object' ? demo(obj[key]) : obj[key];
}
return result;
}

from frontend-interview.

soliderMan avatar soliderMan commented on August 23, 2024

function deepClone(obj){
var rest=JSON.parse(JSON.sringify(obj));
return rest;
}

from frontend-interview.

ankeji avatar ankeji commented on August 23, 2024

from frontend-interview.

aRidiculousBoy avatar aRidiculousBoy commented on August 23, 2024

function deepClone(obj) { if (typeof obj !== 'object' || obj == null) { return obj } var res if (Array.isArray(obj)) { res = [] } else { res = {} } for (const key in obj) { if (obj.hasOwnProperty(key)) { res[key] = deepClone(obj[key]) } } return res }

from frontend-interview.

ainuo5213 avatar ainuo5213 commented on August 23, 2024
function deepClone(obj = {}) {
    if (typeof obj !== "object" || obj == null) return obj;
    let result = obj instanceof Array ? [] : ({});
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            result[key] = deepClone(obj[key]);
        }
    }

    return result;
}

from frontend-interview.

aotemj avatar aotemj commented on August 23, 2024
function deepClone(obj, hash = new WeakMap()) {
  if (obj === null) {
    return obj
  }

  if (obj instanceof Date) {
    return new Date(obj)
  }

  if (obj instanceof RegExp) {
    return new RegExp(obj)
  }
  if (obj instanceof Error) {
    return new Error(obj.message)
  }

  if (typeof obj !== 'object') {
    return obj
  }
  // 防止循环引用
  if (hash.has(obj)) {
    return hash.get(obj)
  }

  hash.set(obj, obj)

  let newObj = Object.create(null)
  const keys = Reflect.ownKeys(obj)
  for (const key of keys) {
    const val = obj[key]
    if (typeof val === 'object') {
      newObj[key] = deepClone(val, hash)
    } else {
      newObj[key] = val
    }
  }

  return newObj
}

from frontend-interview.

ThenMorning avatar ThenMorning commented on August 23, 2024
function deepClone(obj, hash = new WeakMap()) {
  if (obj === null) {
    return obj
  }

  if (obj instanceof Date) {
    return new Date(obj)
  }

  if (obj instanceof RegExp) {
    return new RegExp(obj)
  }
  if (obj instanceof Error) {
    return new Error(obj.message)
  }

  if (typeof obj !== 'object') {
    return obj
  }
  // 防止循环引用
  if (hash.has(obj)) {
    return hash.get(obj)
  }

  hash.set(obj, obj)

  let newObj = Object.create(null)
  const keys = Reflect.ownKeys(obj)
  for (const key of keys) {
    const val = obj[key]
    if (typeof val === 'object') {
      newObj[key] = deepClone(val, hash)
    } else {
      newObj[key] = val
    }
  }

  return newObj
}

Array

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.