How to show the second to last item in a repeating group?

Any idea on how to show the second to last item in a list of things? There is an option to view the last item and the first item but i can’t figure out how to display the second to last. It seems so simple but i can’t figure it out.

I tried “:count -1” but it wouldn’t let me. Thanks for your ideas!

:slight_smile:

1 Like

Reverse the sort order and pick number 2?

Do you know what the default order is so i can reverse it? Is it by created date or something?

This doesn’t work - sorting reorders the list alphabetically, not by the actual position of an element in the list. I could really use a nice way to get the second to last or nth to last item in a list

:items from

Returns a sublist of the list, from the #th item to the end.

52

We’re trying to get the nth from last item. Not all items from n to the end of the list. So second from last item would be the 6th element from a list with 8 items or the 7th item in a list of 9. But we don’t know how many items the list has… Doing List Count - 1 doesn’t work in bubble

Try something like this. This might be similar to what you are looking for.

Oh OK. Then you mean the second item starting from the last one.

Here you are.

image

image

Use the toolbox plugin element Expression.

image

image

3 Likes

An expression is another way to do it. Thanks!!

1 Like

I have to say that it is ridiculous that there is no non-hacky way to get second to the last item

1 Like

You can use the #last item -1

1 Like

@nocodeventure Thanks. I am not sure if I am facing a bug. But it does not work for me.

I found another way to do it but again seems to not work. list:minus(list:#last item):#last item

More correctly:

You can precompute the item number of the second to last item in some_list as:

Set some_state to some_list:count - 1

^^^ this is an expression you can build

Then, the second to last list item is:

some_list:item# some_state’s value

However, you cannot build the expression:

some_list:item# some_list:count - 1

Because:

some_list:item# some_list:count

resolves to an object of some_list’s type and does so before evaluating anything further to the right.

You cannot force Bubble to evaluate (count - 1) in-line with the :item# operator. That’s not a thing you can do.

Now, if some_list is of type number, you CAN construct:

some_list:item# some_list:count - 1

But that expression does not yield the number stored in the second to last item of some_list. That expression yields [edit] the VALUE held in the LAST item in the list minus one. (So If last item holds 10 this yields 9.)

@keith, nice explanation. That is why I think that Bubble may need to provide interface for accessing n-th item from the last position. Although, if only few of the users want it it does not make sense to do it. This page can be an indicator if it is wished or not.

2 Likes

Well, parens in the Expression Builder would solve this too. :wink:

2 Likes