“Klonobjekt in TypeScript” Code-Antworten

Klonobjekt in TypeScript

//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

Typscript -Klonobjekt

import _ from "lodash"

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

Ähnliche Antworten wie “Klonobjekt in TypeScript”

Fragen ähnlich wie “Klonobjekt in TypeScript”

Weitere verwandte Antworten zu “Klonobjekt in TypeScript” auf TypeScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen