It is bizarre why languages you named works in the same manner as Godot’s
Array.find method with exception for Java - is does not have
Array.find method at all. In Java you have to program it yourself.
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfies the testing function, undefined is returned.
Moreover, the name of function you are looking is
indexOf. And to find all indexes in array you still need to write your own functions, in all languages you named.
C# - c# - .indexOf for multiple results - Stack Overflow
Java - did not found anything different from write a custom function using the for loop and iterate through the array to find all matching values.
So answer to your question
shouldn’t this be the default though
is probably no, because again, languages you named does not have this functionality either. You have to simply write a custom function using the for loop and iterate through the array to find all matching values.
Actually, I was not referring directly to the Find method with any of the languages I mentioned but rather the .Filter() which returns a collection of matching values. So apologies for the mix up.
With that said, all mentioned languages provide a way to filter a collection whether it’s via Java’s Stream API, C#'s FindAll() or LINQ .Where() feature set or JS’s built-in Array.prototype.filter().
However, based on your answer and the languages’ documentation, it is clear that their Find method returns the first found occurrence. Sadly, this is not clearly stated in Godot’s offering and can be very misleading the way it’s written.
Thanks for the response.
gonzo191 | 2021-01-08 22:06
In documentation return value is
int so it can’t return array. It also states that
returns its index or -1 if not found. There is no
return indexes, only
Also, functions you are referencing will not give you expected results. I am not familiar with Java, but in C#
filter function. For array
[1, 2, 3, 1, 1, 1, 3] and search value of
1 you will get
[1, 1, 1, 1] (elements itself, not their indexes). So you still have to make your own function to get indexes.
AlexTheRegent | 2021-01-08 23:37