Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript passing arrays to functions by value, leaving original array unaltered

I've read many answers here relating to 'by value' and 'by reference' passing for sending arrays to javascript functions. I am however having a problem sending an array to a function and leaving the original array unaltered. This example llustrates the problem:

function myFunction(someArray) { // any function that makes an array based on a passed array; // someArray has two dimensions; // I've tried copying the passed array to a new array like this (I've also used 'someArray' directly in the code);  funcArray = new Array(); funcArray = someArray;  var i = 0;      for(i=0; i<funcArray.length; i++)     {     funcArray[i].reverse;     }  return funcArray;  } 

I can't understand why anything in this function should alter the original array.

calling this function directly changes the original array if the function call is assigned to a new array:

myArray = [["A","B","C"],["D","E","F"],["G","H","I"]]; anotherArray = new Array();  anotherArray = myFunction(myArray); // myArray gets modified!; 

I tried using .valueOf() to send the primitive:

anotherArray = myFunction(myArray.valueOf()); // myArray gets modified!; 

I have even tried breaking the array down element by element and sub-element by sub-element and assigning all to a new 2-d array and the original array still gets modified.

I have also joined the sub-elements to a string, processed them, split them back into arrays and the original array still gets modified.

Please, does any one know how I can pass the array values to a function and not have the passed array change?

like image 865
Dave Pritlove Avatar asked Jan 23 '13 23:01

Dave Pritlove


People also ask

Are arrays passed by value in JavaScript?

Changing the argument inside the function affects the variable passed from outside the function. In Javascript objects and arrays are passed by reference.

Why does changing an array in JavaScript affect copies of the array?

An array in JavaScript is also an object and variables only hold a reference to an object, not the object itself. Thus both variables have a reference to the same object.

How do you pass an array to a variable in JavaScript?

Method 1: Using the apply() method: The apply() method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.

Does Push change the original array?

It hasn't been changed in any way. When you pass an array as an argument like you are doing in your modify() function, the function argument in the function is just like the b variable in the above example. It's just another variable that points at the same array.


1 Answers

Inside your function there's this:

funcArray = new Array(); funcArray = someArray; 

This won't actually copy someArray but instead reference it, which is why the original array is modified.

You can use Array.slice() to create a so-called shallow copy of the array.

var funcArray = someArray.slice(0); 

The original array will be unaltered, but each of its elements would still reference their corresponding entries in the original array. For "deep cloning" you need to do this recursively; the most efficient way is discussed in the following question:

What is the most efficient way to deep clone an object in JavaScript?

Btw, I've added var before funcArray. Doing so makes it local to the function instead of being a global variable.

like image 85
Ja͢ck Avatar answered Oct 20 '22 00:10

Ja͢ck