In js development, we often encounter the need to determine whether a certain element exists in an array. In fact, there are many ways to determine this, so let’s understand them one by one.
Let’s define an array first.
In this array, we have several types: number, boolean, string, undefined, null, array, object, Date, Symbol, etc. The number 13 appears 2 times.
We are most familiar with
indexOf, after all, it appeared early, it is compatible, and it is easy to use.
If the element exists, the index value of the first occurrence is returned; if the element does not exist in the whole array, -1 is returned.
Just determine if the returned data is -1 to know if the array contains the element.
The counterpart of indexOf is lastIndexOf, which looks for the element from last to first and returns the index of the last one in the array if it exists, or -1 if it does not exist.
Both methods are called in the same way when determining whether a variable exists or not.
1.2 2nd optional parameter
indexOf and lastIndexOf have a second optional parameter, fromIndex, which indicates the index from which the search is performed.
In indexOf, if fromIndex exceeds the length of the array, -1 is returned directly, and if it is negative, the search starts at the last index (arr.length-Math.abs(fromIndex)) and goes backwards.
In lastIndexOf, if fromIndex reaches or exceeds the length of the array, the whole array is searched; if it is a negative number, several indexes are counted from the end to the front (arr.length-Math.abs(fromIndex)), and then the search starts from the front, and if the absolute value of the negative number exceeds the length of the array, -1 is returned directly.
And the indexOf and lastIndexOf are determined in a strictly equal way (===).
indexOf is mainly to find the index value of the element, but we can use the returned index value to indirectly determine if the element exists in the array.
includes method, added in ES7 (ES2016), is specifically designed to determine whether an element exists or not. The return value is true or false, true means it exists, false means it does not exist, simple and clear.
Also, there is a second optional argument to the includes method, fromIndex, which is used in the same way as in indexOf. If fromIndex exceeds the length of the array, -1 is returned directly, and if it is negative, the index is counted from the end to the first (arr.length-Math.abs(fromIndex)), and then the search starts backwards.
So far, we have not judged the latter types, such as Array, Object, Date and Symbol. Let’s now look at the latter elements.
The result is tragic, none of these elements are retrieved in the array. But in fact, they are all true in the array.
This is because both indexOf and includes are determined in a strict equality way (===).
For these types that cannot be retrieved, we will need to write our own functions to determine the special types.
3. find and findIndex
find() and findIndex() allow us to customize the way we judge through callback functions.
3.1 The find method
find() method returns the value of the first element of the array that satisfies the test function provided. Otherwise it returns
The find() method cannot detect undefined elements in an array.
Because the find() method returns undefined for both the absence and the presence of undefined elements, we will have to consider other approaches, which we will discuss later.
For the slightly more complex types above, we need special judgments.
The above judgment code will be used later in the method as well.
3.2 Comparing two elements
We have compared multiple types of elements above, so let’s summarize a little.
First, let’s define a function.
3.2.1 Basic types
For elements of basic types such as string, number, boolean, undefined, null, etc., direct comparison is possible.
3.2.2 NaN data
NaN is determined to be of type number using typeof, but NaN is not equal to any number, including itself.
3.2.3 Function and Date and RegExp
These types can be compared by converting variables to strings.
For object types and arrays, we can split each item and then compare them one by one using the above method.
3.3 The findIndex method
If we want to determine if there is an undefined array, we can use the
findIndex()method returns the index of the first element of the array that satisfies the provided test function. If the corresponding element is not found then -1 is returned.
The other data formats are determined as in find() above.
some() method tests if at least 1 element of the array passes the provided function test. It returns a value of type Boolean.
Note: If you test with an empty array, it will return
falsein any case.
The some() method is used in the same way as the find() method, except that the some() method returns data of type boolean.
filter() method creates a new array containing all the elements of the test implemented by the provided function.
Whether several elements are found or none, the filter() method returns an array whose data is the elements we want.
So we can determine whether the original array contains the elements we want by the length of that array.
There are many ways to find elements in an array, we can choose a more suitable way by the format of the elements in the array. If they are basic types, we recommend using the
includes() method; if the format is more complex, we recommend using the
some() method. Both methods return the boolean type directly, so you can use the result of the method directly without more conversion.