最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout observable array remove after index - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try 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 });
发布评论

评论列表(0)

  1. 暂无评论