Search code examples
knockout.jsknockout-2.0

Copying an observable array


How do I get a copy of items in knockout observable array. Changing the copied items should not affect the original item.

What I want to do is before opening a popup, create a copy of item when the user clicks cancel button reload the original values


Solution

  • You can just use standard JavaScript techniques to clone your array. See the following:

    Copying array by value in JavaScript

    So, you can copy as follows:

    var newArray = viewModel.observableArray().slice(0);