You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Accessing the last item of an array is a pretty common task, but it's not easy to do with the existing jsonget, jsonset, etc. operators. You first have to get the last index of the array with [<jsondata>jsonindices[]maxall[]] and then store that in a variable (let's call it lastIndex), then do [<jsondata>jsonget<lastIndex>]. It would be nice to have an easy way to refer to "the last item in the array" without knowing the array's length ahead of time.
Describe the solution you'd like
In any situation where one of the jsonget family of operators would count a numeric as an index into an array, any index that's a negative number would be counted as (index + array.length). So an index of -1 (negative one) would count as "array.length - 1", i.e. the last item in the array. An index of -2 (negative two) would count as the next-to-last item in the array, and so on.
Describe alternatives you've considered
An alternative could be to use a suffix like fromend, so that jsonget:fromend[1] would mean "the last item of the array". But there are already so many suffixes, and most programming languages already have the concept of "-1 means the last item" built in, so the concept is familiar to anyone who's done any programming before. Plus, it's not clear whether fromend[1] or fromend[0] should mean "the last item", whereas -1 meaning "last item" is pretty universal across languages and is therefore easy to remember.
Additional context
This idea came out of a discussion at #7742 (comment).
The text was updated successfully, but these errors were encountered:
But the indexes coming in are strings, not integers; it turns out that Javascript lets you write array["3"] and it works the same as array[3], getting the fourth item in the array. But when you have an array of length, say, 5, and you add "-1" and 5, you get "-15", not -4. I've found a good way to deal with this issue and I'll have a PR soon.
Is your feature request related to a problem? Please describe.
Accessing the last item of an array is a pretty common task, but it's not easy to do with the existing
jsonget
,jsonset
, etc. operators. You first have to get the last index of the array with[<jsondata>jsonindices[]maxall[]]
and then store that in a variable (let's call itlastIndex
), then do[<jsondata>jsonget<lastIndex>]
. It would be nice to have an easy way to refer to "the last item in the array" without knowing the array's length ahead of time.Describe the solution you'd like
In any situation where one of the
jsonget
family of operators would count a numeric as an index into an array, any index that's a negative number would be counted as (index + array.length). So an index of -1 (negative one) would count as "array.length - 1", i.e. the last item in the array. An index of -2 (negative two) would count as the next-to-last item in the array, and so on.Describe alternatives you've considered
An alternative could be to use a suffix like
fromend
, so thatjsonget:fromend[1]
would mean "the last item of the array". But there are already so many suffixes, and most programming languages already have the concept of "-1 means the last item" built in, so the concept is familiar to anyone who's done any programming before. Plus, it's not clear whetherfromend[1]
orfromend[0]
should mean "the last item", whereas -1 meaning "last item" is pretty universal across languages and is therefore easy to remember.Additional context
This idea came out of a discussion at #7742 (comment).
The text was updated successfully, but these errors were encountered: