0 votes

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?

Example

var array = [1,2,3,2,5,2,1]
array.find(2)
result indices: array[1], array[3], array[5]

array.find(5)
result indices: array[4]
Godot version 3.2.3
in Engine by (122 points)

1 Answer

0 votes

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# - https://stackoverflow.com/questions/6865419/indexof-for-multiple-results
Javascript - https://stackoverflow.com/questions/20798477/how-to-find-index-of-all-occurrences-of-element-in-array
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.

by (800 points)

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.