Although the find index, and find methods might work okay in many situations there are some sort coming s with the method. For one thing the method will always return the first element from left to right rather than the other way around. Also there are some steps that should typically be taken before finding an element in an array such as sorting, and filtering the array. For the most part the find method might be a good choice when it comes to getting an element in an array that just has a single unique value, rather than situations in which I want a list of elements where the first element in the list would be a best match.
In this example I am now dealing with an array of objects, however the basic idea is still the same. I just call the find index method off of the array, and pass a test function. In the body of the test function I am then just accessing a standard property of each objects that should be there, and comparing it to a desired value.
In this example I once again have an array of objects where each object contains properties for a student at a school. This time though I have some additional properties such as the grade number rather than just a letter grade, and a string that is the subject that the student seems to show the most interest in over all others. So then say you would like to find a student that would be the best fit for work that has to do with a specific subject such as science. There is just going by the grades and that alone, but there is also taking into account if someone shows interest in science or not regardless of poor or great grades over all.
So then in this example I am using the array map method to create an array of objects from the source array of student objects. The array map method will create and return a new array, so node of this will change the state of the source array. Each object in this weight objects array will contain a weight value, and an index value for the student objects array. I am then using the array sort method as a way to change the order of these weight objects so the the weight object with the highest weight value is the first element.
I now have an array of objects with index values where the first object is the best student for this kind of query. The student selected then is not the student with the best over all grade, but the result of an okay grade with a subject bonus added to a weight value because of an active interest in the subject of science. The next runners up though do still have high grades.
Now that I have the basics of the find index array method out of the way, and also touched base on some examples that have to do with using other methods a s a way to create and sort an array of elements index values, maybe now I should write about some use case examples of all of this. There are all kinds of projects that will end up involving finding one object index value, or an array of index values, in some kind of collection. So in this section I will be going over at least a few examples of using the array find index method, or by one way or another getting one or more index values.
When creating a module of some kind I might want to have some kind of public method that can be used to find an single object in a collection that has a given value, for a given object property name. In threejs for example there are methods for getting a reference to an object by an id value, and then there is another one to get a reference by name string. There is then having a more generic tool that is something like that than can be use to get a reference to an object, or the index value for that object, for any property and value for that property.
When it comes to making all kinds of games there is often a situation in which there needs to be a process to go about purging dead units from a collection of some kind. Often there may be a system that involves creating and removing objects as needed, but often I think the best way is to have some kind of system for creating a fixed pool of objects that are reused over and over again.
So then the find index array prototype method will work okay if we are taking about finding just one element in an array. However in situations in which there may be more than one candidate for a search query of some kind then the method will fall short.