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
Post a Comment