0
var depe = {}

var array1 = [];
array1.push('value1');

var array2 = [];
array2.push('value1a');
array2.push('value2');

depe['key1'] = array1;
depe['key2'] = array2;   // now variable depe contains {"key1":["value1"],"key2":["value1a","value2"]}


var temp = depe['key2'];  // now variable temp contains ["value1a","value1a"]
temp[temp.length] = 'newValue' // now variable temp contains ["value1a","value1a","newValue"]

After execution of last line variable depe contains

{"key1":["value1"],"key2":["value1a","value2","newValue"]}

I don't want to be updated in variable depe. How do I resolve it ?

Jenz
  • 8,280
  • 7
  • 44
  • 77
Prasad V S
  • 262
  • 4
  • 17

4 Answers4

4

By default, arrays are passed around by reference so if you assign var temp = depe['key2']; and then add another item to temp it is changing the one and only one array so it will show in both places.

To make a separate shallow copy of an array that you can change, you have to make an explicit copy which you can do with array.slice(0):

// get a copy of the array in depe['key2']
// that I can change independently of what is in depe
var temp = depe['key2'].slice(0);
temp[temp.length] = 'newValue';
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You have to clone objects in javascript.

Please read: http://jsperf.com/cloning-an-object/2

or

function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

from: How do I correctly clone a JavaScript object?

Community
  • 1
  • 1
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • Except what this OP wants a copy of is an array which can be shallow copied with just `array.slice(0)`. – jfriend00 Apr 30 '14 at 07:42
-1

you need to clone your array

    function cloneArray(array){
      var newArray = [];
      for(var i = 0; i < array.length; i++){
       newArray[i] = cloneValue(array[i]);
      }
      return newArray;
    }

to clone a value

   function cloneValue(value){
     if (typeof value != 'object')
        return value;
     else {
       var newObj = {};
     for (var prop in value){
      newObj[prop] = value[prop];
    }
    return newObj;
  }
}

and use it like this

 var arr2 = cloneArray(arr1);
Bellash
  • 7,560
  • 6
  • 53
  • 86
-1

i think u should use slice() method to get a clone of depe array. you can read this for your reference : Copying array by value in JavaScript OR u can make this prototype function:

Array.prototype.clone = function() {
return this.slice(0);

};

so in line : var temp = depe['key2'];

u can use depe['key2'].clone() instead of depe['key2'].slice(0)

Hope this help you.

Community
  • 1
  • 1
RicoWijaya
  • 15
  • 5
  • It is a bit dangerous to add enumerable methods to the Array object because if/when someone does a `for (key in array)` enumeration of the array (which isn't the proper way to enumerate an array, but is done far too often), then `"clone"` will come up as an item in the array. – jfriend00 Apr 30 '14 at 07:55