Clearing an observableArray in knockoutjs


I recently had the need to remove all items from a multidimensional observable array and discovered that doing this by reinitialising means you need to call ko.applyBindings(…) again. For performance reasons this isn’t something you want to be doing to often. The correct way to do this is call the RemoveAll() method directly on the observableArray itself.

var viewModel = {
    customers: ko.observableArray([]),
    moreprops: ko.observable('whatever');


//If we now need to clear down the array (in our case this was to restart a quiz) 
//you do the following

//You can now add items back into the array without needing to call applyBindings again.

4 thoughts on “Clearing an observableArray in knockoutjs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s