Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip item in foreach knockout js array?

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>
like image 377
Phoenix_uy Avatar asked Mar 08 '13 17:03

Phoenix_uy


1 Answers

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>
like image 138
ebram khalil Avatar answered Sep 20 '22 08:09

ebram khalil