Hi all
Abp's solution will work well if you want the index as a way to
generate a unique ID.
If you actually just want the numerical position of the item in your
array, the reason KO doesn't already provide an index is that it
frequently would be wrong to do so. The whole idea of the "foreach"
binding is to let you modify your array later and only rerender the
templates for the new ones you've added. If your templates depend on
knowing the index of the item they're rendering, how does this make
sense when items are inserted or deleted later (effectively changing
the numerical positions of the output) and those templates aren't
rerendered?
If you know you're not going to be inserting or removing things later,
you'll get the same result by not using "foreach" outside your
template, but instead using a {{foreach}} loop *inside* your template,
which will give you the index and value of each item in your array.
Does this make sense, or have I misunderstood your request? If there's
a use case where you think this would definitely make sense and it
isn't already covered, please let us know!
Thanks
Steve