Skip item in foreach knockout js array?

2019-06-20 03:50发布

i have this question about a foreach in knockout js and the first item. I want to skip the first one and iterate over the next items.

The main issue is that i wanna do something like this:

<div data-bind="text: ItemsArray[0].someProperty"></div>
<div data-bind="foreach: ItemsArray"> <!-- here i must skip the first item -->
     <div data-bind="text: someProperty"></div>
</div>

1条回答
Melony?
2楼-- · 2019-06-20 04:26

I don't think knockoutJS provides a function to skip a specific element in an Array, but you can use a small trick.

If you want to skip only the first item, you can use the $index property:

<div data-bind="text: ItemsArray[0].someProperty"></div>
<div data-bind="foreach: ItemsArray">
<!-- ko if: $index() != 0 -->
     <div data-bind="text: someProperty"></div>
 <!-- /ko -->
</div>
查看更多
登录 后发表回答