How can I use wherecontains() for multiple conditions?
For example, let's say I have an array of my cdt type Person, called people:
[ name="Ben", age=20 ]; [ name: "Sam", age:50 ]
I could write wherecontains("Ben", people.name). This would return the first element in the array.
But what if the array looked like this:
[ name="Ben", age=20 ]; [ name: "Ben", age:50 ]
I still want to get the first index but wherecontains("Ben", people.name) would return both indices. How can I add a condition to check name AND age?
Discussion posts and replies are publicly visible
Hello Fabian ,
Let me think of this but one way to get this is to run a for each and in the expression you check for the name and age and return the fv!index. If not return {} or null or -1 .
If you still insist with the where contains, well you can make two arrays for each evaluation, and then make a difference, or intersection so that gives you the indexes which match both conditions
Hope this helps
Jose
I think this would be a good use case for the filter() function. You create a separate helper function which implements the conditions and filter() will return only matching values from your list.
It could if his final goal is to have the list of object and he do not care about the the indexes.
But yeah that is a good option
Thanks for your help and Stefan Helzle. I'm going to go with the filter() function.
Please try below logic, It will work for you
wherecontains(tostring("Ben"),cast(103,people.name))
I would add that a!forEach is so powerful that you can basically use it to replace whereContains() altogether, and especially so in more complex use cases like this.
with( local!peopleArray: { { Name: "Mary", Age: 22 }, { Name: "Ben", Age: 22 }, { Name: "Ben", Age: 55 }, { Name: "Mike", Age: 39 } }, a!forEach( local!peopleArray, if( and( fv!item.Name = "Ben", tointeger(fv!item.Age) = 22 ), fv!index, {} ) ) )