Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDEA] Allow negative indexes in jsonget/set/etc operators for array access from end #7839

Closed
rmunn opened this issue Nov 15, 2023 · 2 comments · Fixed by #7849
Closed

[IDEA] Allow negative indexes in jsonget/set/etc operators for array access from end #7839

rmunn opened this issue Nov 15, 2023 · 2 comments · Fixed by #7849

Comments

@rmunn
Copy link
Contributor

rmunn commented Nov 15, 2023

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).

@Jermolene
Copy link
Owner

Thanks @rmunn I think that's a great idea.

@rmunn
Copy link
Contributor Author

rmunn commented Nov 21, 2023

This is turning out slightly more complicated to implement than I thought. At first I thought I could just write:

if($tw.utils.isArray(item) && index < 0) { index = index + item.length; }

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants