“Objek klon dalam naskah” Kode Jawaban

Objek klon dalam naskah

//1.Shallow copy:
let Copy = {...yourObject}
//2.Deep Copy: a. through recusive typing functionality:
let Cone = DeepCopy(yourObject);
public DeepCopy(object: any): any
{
  if(object === null)
  {
    return null;
  }
  const returnObj = {};
  Object.entries(object).forEach(
  ([key, value]) =>{
    const objType = typeof value
  if(objType !== "object" || value === null){
     returnObj[key] = value;
  }
  else{
  	returnObj[key] = DeepCopy(value);
  }
}
//b.Hardway: repeat the following expanstions for all complex types as deep as you need
let Copy = {...yourObject, yourObjsComplexProp: {...yourObject.yourObjsComplexProp}}
TheCodeTrooper

Objek klon naskah

import _ from "lodash"

const obj = { foo: "bar" }
const clone = _.cloneDeep(obj);
Jens

Jawaban yang mirip dengan “Objek klon dalam naskah”

Pertanyaan yang mirip dengan “Objek klon dalam naskah”

Lebih banyak jawaban terkait untuk “Objek klon dalam naskah” di TypeScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya