javascript - Set Array element from another array element -


i want set value of array element array element in javascript. both elements changed! how change arr[1]?

arr[0] = {i: 0}; arr[1] = arr[0]; arr[1]['summ'] = 0; 

results:

(2) [object, object] 0: object   i: 0   summ: 1 1: object   i: 0   summ: 0 

because both of them have same reference, should copy value of object second object has different reference. try trick:

arr[1] = json.parse(json.stringify(arr[0])); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -