How to clone an object in TypeScript

2 Answers

0 votes
const user = {
  id: 90093,
  name: 'Rog'
};
 
const clone_user = { ...user };
 
console.log(clone_user.id);
console.log(clone_user.name);
 
 
 
/*
run:
           
90093 
"Rog" 
     
*/

 



answered Mar 13, 2022 by avibootz
0 votes
const o = {
  id: 74762,
  lang: "typescript",
};
  
const cloneObject = { ...o };
  
console.log(cloneObject);
  
o.id = 9817;
  
console.log(cloneObject);
  
  
      
      
/*
run:
      
 {
  "id": 74762,
  "lang": "typescript"
} 
{
  "id": 74762,
  "lang": "typescript"
} 
      
*/

 



answered Mar 13, 2022 by avibootz

Related questions

1 answer 112 views
3 answers 141 views
2 answers 185 views
185 views asked Mar 13, 2022 by avibootz
1 answer 188 views
2 answers 191 views
1 answer 120 views
120 views asked Jun 4, 2016 by avibootz
...