How to Copy An Array of Arrays in JavaScript

When we want to grab data from an array, we may accidantally change the value in the original array. Then, when we can't access the original array. This is problematic.

For Example

var arrOriginal = [1,2];
var arrCopy = arrOriginal;
arrCopy[0] = 10;
console.log(arrOriginal);
// shows [10,2]
The original array is modified by modifying a supposedly different array. This can also happen with array pop, splice and shift.

One way to solve this problem is to use array.push method to assign values to the new arrray.
 
var arrOriginal = [1,2,3];
var arrCopy = [];

arrCopy.push(arrOriginal[0]);
arrCopy.push(arrOriginal[1]);
arrCopy.push(arrOriginal[2]);

arrCopy[0] = 10

console.log(arrOriginal);
// shows [1,2,3]
Or, we can use the spread operator.
var arrOriginal = [1,2,3];
var arrCopy = [...arrOriginal];

arrCopy[0] = 10;
 
console.log(arrOriginal);
//shows [1,2,3]
console.log(arrCopy);
//shows [10,2,3]

Now, here is the problem!

When copy an array using eighter the push method or the spread operator, and if there are any inner arrays inside of the original array, everytime the elements in the inner arrays get changed, the elements of the original array get changed, too.
var arrOriginal = [[1,2],[3,4],5];
var arrCopy = [...arrOriginal];

arrCopy[1][0] = 10;

console.log(arrOriginal);
//shows [[1,2],[10,4],5], changed.
console.log(arrCopy);
//shows [[1,2],[10,4],5]

I made a function to copy a array of arrays.

var arrOriginal = [[1,2],[3,4],5];
function copyArray(array){
	var newArray = [];
	array.forEach(x=>{
		if(Array.isArray(x))
		newArray.push([...x]);
		else
		newArray.push(x);
	});
	return newArray ;
};

var newArr = copyArray(arrOriginal);

newArr[1][0] = 10;
console.log(arrOriginal);
//shows [[1,2],[3,4],5], unchanged.
console.log(newArr);
//shows [[1,2],[10,4],5]
Well, the above function will not successfully copy an array if the array has 3 or more layers.
var arrOriginal = [[[1,7],2],[3,4],5];

function copyArray(array){
	var newArray = [];
	array.forEach(x=>{
		if(Array.isArray(x))
		newArray.push([...x]);
		else
		newArray.push(x);
	});
	return newArray ;
};

var newArr = copyArray(arrOriginal);

newArr[0][0][0] = 10;
console.log(arrOriginal);
//shows [[[10,7],2],[3,4],5], changed.
console.log(newArr);
//shows [[[10,7],2],[3,4],5]

For a 3-layer array, we can use the following to copy it.

var arrOriginal = [[[1,7],2],[3,4],5];

function copyArray(array){
	var newArray = [];
	array.forEach((x,i)=>{
		if(Array.isArray(x)){
			newArray.push([]);
			x.forEach(y=>{
				if(Array.isArray(y))
				newArray[i].push([...y]);
				else
				newArray[i].push(y);
			});
		}else
		newArray.push(x);
	});
	return newArray ;
};

var newArr = copyArray(arrOriginal);

newArr[0][0][0] = 10;

console.log(arrOriginal);
//shows [[[1,7],2],[3,4],5], unchanged.
console.log(newArr);
//shows [[[10,7],2],[3,4],5]

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser