I have 2 observablearray. When I copy items from observablearray1 to observablearray2, and change value in observablearray2, in changes in observablearray1 too.
var viewModel = (function() {
var
array1 = ko.observableArray(['John', 'Joe', 'Jim']),
array2 = ko.observableArray();
// copy
array2(array1());
array2()[2] = 'Mary';
return {
array1: array1,
array2: array2
}
})();
ko.applyBindings(viewModel);
console.log(viewModel.array1());
console.log(viewModel.array2());
http://jsfiddle.net/xveEP/69/
Results:
array 1
John
Joe
Mary
array 2
John
Joe
Mary
How to fix it? Thanks
Copy the elements of the array instead of the array itself:
// copy
for (var i = 0; i < array1().length; i++) {
array2().push(array1()[i]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With