B4J Question [BANano] [SOLVED] How to use the .Find Array Function?

Mashiane

Expert
Licensed User
Longtime User
Ola

I'm a little confused, please advise as to the right way of doing this.

1634809754476.png



I have defined this callback..

B4X:
private Sub Finder(value As Object, index As Int, Items As List)
    
End Sub

BANano.Find expects the list name, granted, and the callback method as a "String" - so the assumption is that its in the same code module? So I just write "Finder" there, the name of my sub?

Also inside the callback method, I have to write my own method to check the conditions I assume?

Thanks.
 
Solution
I just added these functions to match what JavaScript can do, but I find hem very counterintuitive and never use them. I just loop through the list and do my stuff. Anyway, something like this:

B4X:
Sub IsAlain(value As Object, index As Int, Arr As List) As Object 'ignore
    Dim tmpObj As Map = value
    Return (tmpObj.Get("name") = "Alain")
End Sub

Sub YounderThan40(value As Object, index As Int, Arr As List) As Object 'ignore
    Dim tmpObj As Map = value
    Return (tmpObj.Get("age") < 40)
End Sub

' HERE STARTS YOUR APP
Sub BANano_Ready()
    Dim myList As List
    myList.Initialize
    myList.Add(CreateMap("name": "Alain", "age": 47))
    myList.Add(CreateMap("name": "Jos", "age": 35))
    myList.Add(CreateMap("name": "Carine"...

alwaysbusy

Expert
Licensed User
Longtime User
I just added these functions to match what JavaScript can do, but I find hem very counterintuitive and never use them. I just loop through the list and do my stuff. Anyway, something like this:

B4X:
Sub IsAlain(value As Object, index As Int, Arr As List) As Object 'ignore
    Dim tmpObj As Map = value
    Return (tmpObj.Get("name") = "Alain")
End Sub

Sub YounderThan40(value As Object, index As Int, Arr As List) As Object 'ignore
    Dim tmpObj As Map = value
    Return (tmpObj.Get("age") < 40)
End Sub

' HERE STARTS YOUR APP
Sub BANano_Ready()
    Dim myList As List
    myList.Initialize
    myList.Add(CreateMap("name": "Alain", "age": 47))
    myList.Add(CreateMap("name": "Jos", "age": 35))
    myList.Add(CreateMap("name": "Carine", "age": 41))
       
    Log(BANano.Find(myList, "IsAlain"))
    Log(BANano.Find(myList, "YounderThan40"))
   ...

Alwaysbusy
 
Last edited:
Upvote 1
Solution

alwaysbusy

Expert
Licensed User
Longtime User
it would have been awesome if on the find, one could specify
I was also surprised one can't pass any arguments. However, a quick workaround using global variables could work around it somewath:

B4X:
' global
Private TestAge As Int
...
Sub YoungerThan(value As Object, index As Int, Arr As List) As Object 'ignore
    Dim tmpObj As Map = value
    Return (tmpObj.Get("age") < TestAge)
End Sub
...
TestAge = 40
Log(BANano.Filter(myList, "YoungerThan"))

Alwaysbusy
 
Upvote 0
Top