What item # in a collection am I in?

Is there a way to determine what item number of a collection that the iteration is in?

This comes in handy a lot for displaying borders, punctuation, etc.

Usually, I need to know if the item I am working on is the first or last item in a collection. Is there a simple way to get this and then compare to the length?

Hey @kevinwasie ,
yes this is totally doable. Its hidden inside context.item data.
In the Formula Explorer go to the Data tab, and open the Context. You will see the index property.

1 Like

@aurelie Thank You!

One step further, how would I then go about determining the item # in a filtered collection?

For instance, let’s say that I have a collection of cars, then I filter by brand.

Now, I want to know if that item is the last item in the filtered brand. Is that possible with no code?

The index I pointed you will be the index on the bound data. So if you bind to the filtered list, index will be the index from it.
So if I understand correctly, this is the index you want.
If you wish to access the index from the non filtered list, you need to modify your data and add the original index in it (by transforming your data with a global workflow or by adding it on your backend).

Does this help?

If not, feel free to describe me your usecase so that I can better understand what you are trying to achieve :slight_smile:

I have a collection, that has an array in it. I am filtering that array based on a key.

I am then using that filtered array as a collection for repeating elements. I want to know if I am in the last item of the filtered array.

Here it is:

property_table

  • property_id
  • address
  • users
    • user_id
    • type (enum)

I have the collection, called All Properties, then inside that collection item, I am adding another collection, of the user array that has all of the users of the property.

I am then filtering that user array based on type.

When I go to the context, it gives me the full length of the collection, not just the filtered collection, which would be only users of a certain type.

Is that clear?

So if i understood correctly, accessing the index is fine now, but you can’t access the length right?
You are binding your User array, then use the filter (inside the formula popup) here to only pick the data you want?

Sadly this is right now not possible in no code. I will ask our CPO if we can find an easy way to make it accessible. Having access to a length property in the same place as the index property will be enough?

For information, for which usecase do you need to access the length/know if this is the last item?

In the meantime you can still manage to do it by custom javascript

Yes.that is correct. I cannot access the length of the filtered array.

For this use case, I need to know if we are in the last item for display purposes.

I’m displaying a list of items horizontally, I need a border between them, but do not want a border on the left of the first item, or right of the last item.

How do I access the filtered collection to get the length in Javascript? thank you.

I figured out how to get the length of the filtered item with no code … I just had to create the same filter on the parent array. So, a little bit of double work, but that’ll do. Thanks!

More to this though … isnt that data important to have so that you can display an error or empty collection message if the filtered list is empty?

In that scenario, there will be no way to gather the parent element since its from outside the context.

I figured out how to get the length of the filtered item with no code … I just had to create the same filter on the parent array. So, a little bit of double work, but that’ll do. Thanks!

Yes this was the way to go, this is why maybe create a variable or a collection to store this value can be a good idea. But this is not possible in your usecase because the data are nested inside a repeat right? (repeat inside repeat?)

More to this though … isnt that data important to have so that you can display an error or empty collection message if the filtered list is empty?

Yes good point. I will let know our CPO, see if we can find a way to make it work.

Yes. I am trying to do this inside of a nested repeat. So, changing a variable wouldn’t work. I need access to the data inside the item context.

Getting from the parent works, but it would be simpler if the length was simply available on filtered data. Thanks for your help.

Would using this index # (thank you for bringing my attention to this btw) be the proper/most elegant way to limit the number of collection items I display from a list?

For instance if my list has 10 items but I only want to display the first 5…it seems to work if I bind the collection item display variable to a formula resulting in true if index <6, but perhaps I’m missing a “better” way to achieve this result?

Thanks!

Nice that this topic pop up, as now you have access to the list of items inside context :slight_smile:
Did you use it?

1 Like

Doing so, you will still render all your item, and hide some. This can lead to performance issue if your list is very long.
There are two solutions to your problem

  • Bind your container items to a subarray of your items, using the slice method
  • Paginate your collection