Why the Array Find only return the first found index of the array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gonzo191

I find it bizarre that Array.Find(…) method only finds the first matched entry rather than all matching entries. Coming from other languages such as C#, Java, JS, etc. where the find method (called filter) produces a list/array of found entries, I find this behaviour is not what I would’ve expected if my array has duplicates.

Now sure, I could simply write a custom function using the for loop and iterate through the array to find all matching values, but shouldn’t this be the default though?


var array = [1,2,3,2,5,2,1]
result indices: array[1], array[3], array[5]

result indices: array[4]
:bust_in_silhouette: Reply From: AlexTheRegent

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.

C# Array.Find
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.

Javascript Array.prototype.find()
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
Javascript - javascript - How to find the indexes of all occurrences of an element in array? - 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 index.

Also, functions you are referencing will not give you expected results. I am not familiar with Java, but in C# FindAll will return elements, not indexes. Same result will be fpr Javascript’s 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