Last updated: Feb 27, 2024
Reading timeยท3 min
To create a deep copy of an array in TypeScript:
JSON.stringify()
method to stringify the array.JSON.parse()
method to parse the string back to an array.const arr = [ { id: 1, address: { country: 'Germany' } }, { id: 2, address: { country: 'Chile' } }, { id: 3, address: { country: 'UK' } }, ]; /** * ๐๏ธ const copy: { id: number; address: { country: string; }; }[] */ const copy = JSON.parse(JSON.stringify(arr)) as typeof arr; console.log(copy);
We used the JSON.stringify method to convert the array to a JSON string.
console.log(typeof JSON.stringify([])); // ๐๏ธ "string"
As a result, all of the references to the objects in memory are lost.
The next step is to parse the JSON string back to an array by using the JSON.parse method.
// ๐๏ธ true console.log(Array.isArray(JSON.parse(JSON.stringify([]))));
At this point, we have a deep copy of the original array, but it's typed as
any
, so we need to use a
type assertion
to set its type to the type of the original array.
const arr = [ { id: 1, address: { country: 'Germany' } }, { id: 2, address: { country: 'Chile' } }, { id: 3, address: { country: 'UK' } }, ]; /** * ๐๏ธ const copy: { id: number; address: { country: string; }; }[] */ const copy = JSON.parse(JSON.stringify(arr)) as typeof arr; console.log(copy);
If you now mutate the copied array, you won't change the contents of the original array.
const arr = [ { id: 1, address: { country: 'Germany' } }, { id: 2, address: { country: 'Chile' } }, { id: 3, address: { country: 'UK' } }, ]; const copy = JSON.parse(JSON.stringify(arr)) as typeof arr; copy.pop(); console.log(copy.length); // ๐๏ธ 2 console.log(arr.length); // ๐๏ธ 3
We used the pop()
method to
remove the last element from the
copy
array but the original array remains unchanged.
I've also written an article on how to create a deep copy of an object.
Alternatively, you can use the clonedeep
method from the lodash
module.
Open your terminal in the root directory of your project and install the lodash.clonedeep package with the following 2 commands:
npm i lodash.clonedeep npm i --save-dev @types/lodash.clonedeep
Now we can import and use the cloneDeep
method.
import cloneDeep from 'lodash.clonedeep'; const arr = [ { id: 1, address: { country: 'Germany' } }, { id: 2, address: { country: 'Chile' } }, { id: 3, address: { country: 'UK' } }, ]; /** * ๐๏ธ const copy: { id: number; address: { country: string; }; }[] */ const copy = cloneDeep(arr); console.log(copy);
The cloneDeep
method takes a value as a parameter, recursively clones it and
returns the result.
You can test removing an element from the copied array and comparing the length of the two arrays.
import cloneDeep from 'lodash.clonedeep'; const arr = [ { id: 1, address: { country: 'Germany' } }, { id: 2, address: { country: 'Chile' } }, { id: 3, address: { country: 'UK' } }, ]; const copy = cloneDeep(arr); copy.pop(); console.log(copy.length); // ๐๏ธ 2 console.log(arr.length); // ๐๏ธ 3
I've also written an article on how to get the type of the array elements from an array type.