xxxxxxxxxx
// Object to be copied
const originalObject = { name: "John", age: 30 };
// Method to create a copy of the object
function copyObject(obj: object): object {
return { obj };
}
// Creating a copy of the original object
const copiedObject = copyObject(originalObject);
console.log(copiedObject); // Output: { name: "John", age: 30 }
xxxxxxxxxx
const sourceObj = { foo: 'bar', baz: 'qux' };
// Copy the object
const copiedObj = Object.assign({}, sourceObj);
console.log(copiedObj); // Output: { foo: 'bar', baz: 'qux' }
xxxxxxxxxx
//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}}
xxxxxxxxxx
// Makes a deep clone, might not work on everything though
const copy = structuredClone(value);