JSON.parse(JSON.stringify(o))
import * as _ from "lodash";
var comma = _.cloneDeep(kitty);
FOR JAVASCRIPT:
- Shallow Copy:
object2 = object1
- Deep Copy:
object2 = {...object1} //for arrays: array2 = [...array1]
- Deep Copy (for Objects with nested Objects):
object2 = JSON.parse(JSON.stringify(object1));
- Deep Copy (for objects with nested 'Date object' OR functions):
1- npm i loadash
2- const _ = require('loadash');
3- object2 = _.cloneDeep(object1);
function copy(arr1, arr2) {
for (var i =0; i< arr1.length; i++) {
arr2[i] = arr1[i];
}
}
copy(arr1, arr2)
var mudge = JSON.parse(JSON.stringify(kitty));
// shallow copy :: modification in original array
var newEmployee = employee;
// Deep copy :: no modification in orifinal array
var newEmployee = JSON.parse(JSON.stringify(employee));
var knuckle = Object.assign({}, kitty);