-
Notifications
You must be signed in to change notification settings - Fork 229
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
$indexOf function for array enhancement request #187
Comments
These additions may suffice:
|
There are a couple of problems here. First, [
{"firstName": "Bill", "lastName": "Williams"},
{"firstName": "Bill", "lastName": "Smith"}
] If my JSONata expression is [
{
"firstName": "Bill",
"lastName": "Williams"
},
{
"firstName": "Bill",
"lastName": "Smith"
}
] But, if you do What I think you want is to record the indices and then do something with them, e.g., this contrived example: $[$indexOf($, firstName="Bill")].lastName In this case, the But here is the rub. In order to implement You are pointing out an interesting quirk in JSONata. Although a predicate expression can be used to select elements from an array, there doesn't seem to be a way to actually evaluate it to get the indices. So my analysis is that you'd either need a way to add user macros or it would have to be implemented as a builtin operator. So, for example, let's say that the predicate expression was preceded by a
But again, this doesn't exist at the moment. I'm just trying to explain why, as far as I can tell, it cannot achieved by binding a function. It is more integral than that. But perhaps @andrew-coleman sees a way around this... |
Yes, it is related. Thank you. I could use your approach with # if that is the desired direction. In my case, I doubt we could use the macro as we have only a single String holding what we want JSONata to evaluate as a "rule". I was also thinking something modeled after JavaScript Array.findIndex where we would pass a filter function might work well. The $filter method seems to take an array and a function -- perhaps just a copy of $filter that returns the indices of the cells that match the comparator function? I hope we can find some way to provide access to indices. Thanks for the detailed feedback. |
Note, the |
I wanted to leverage the location of elements in an array to do something, but couldn't figure out how to get the index of a particular array element matching search criteria. I would like to see something like:
Above are listed in order of preference but would be happy with any of them.
Also posted here if you'd prefer I just use a function:
https://stackoverflow.com/questions/49655474/how-to-get-index-of-element-in-array-in-jsonata
The text was updated successfully, but these errors were encountered: