I'm looking for a way in knockout to remove all the elements in an observable array which e after a given index.
A for-loop to do that is inefficient since removing one element at a time triggers change notifications with every removal. Is there anything out of the box ?
I'm looking for a way in knockout to remove all the elements in an observable array which e after a given index.
A for-loop to do that is inefficient since removing one element at a time triggers change notifications with every removal. Is there anything out of the box ?
Share asked Apr 3, 2014 at 9:07 Diana IonitaDiana Ionita 3,3283 gold badges29 silver badges44 bronze badges1 Answer
Reset to default 6Try this (note that HowMany is optional and if you don't specify it, all the items after StartIndex will be removed):
myObservableArray.splice(StartIndex, HowMany)
If you need to remove items that have certain properties, you can pass a function returning a boolean value to knockout's remove function, for example:
myObservableArray.remove(function(item) { return item.property > YourValue })
+ Quote from knockout docs: LINK
Normally, an observableArray notifies its subscribers immediately, as soon as it’s
changed. But if an observableArray is changed repeatedly or triggers expensive updates,
you may get better performance by limiting or delaying change notifications. This is
acplished using the rateLimit extender like this:
// Ensure it notifies about changes no more than once per 50-millisecond period
myViewModel.myObservableArray.extend({ rateLimit: 50 });