Skip item in foreach knockout js array?

2019-06-20 04:32发布

问题:

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:

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>