
How to use the JavaScript findIndex (Array method)
How to use the JavaScript findIndex (Array method)
Learn how to use the JavaScript Array method called findIndex to return the index of the first element in an array that meets a specific criteria. We’ll cover 2 examples and also demonstrate how to use the “this” Parameter when calling the findIndex method.
Array findIndex() method:
https://www.techonthenet.com/js/array…
JavaScript Tutorials:
https://www.techonthenet.com/js/index…
Don’t forget to subscribe to our channel for more great Excel tutorials!
Visit us at: https://www.techonthenet.com
or follow us on:
Facebook: https://www.facebook.com/techonthenetcom
Twitter: https://twitter.com/tech_on_the_net
Pinterest: https://www.pinterest.com/techonthene…
Content
0.03 -> find index is an array method in
2.25 -> JavaScript the find index method is used
6.27 -> to return the index of the first element
8.13 -> in the array that meets a specific
10.5 -> criteria please note that the first
13.23 -> element of an array has an index of 0
15.45 -> the second has an index of 1 and so on
18.27 -> this will be important for understanding
20.16 -> the index value that find index returns
23.449 -> to illustrate how the find index method
26.22 -> works let's look at a few examples
28.91 -> before we get started we'll just point
31.23 -> out that we are declaring this window
32.969 -> variable because we are executing the
34.95 -> JavaScript code using nodejs for the
37.95 -> purposes of this tutorial and are not
40.32 -> running the JavaScript code within a web
42.149 -> browser such as Chrome
43.789 -> we'll start by declaring an array object
46.62 -> called t OT an array that has five
48.69 -> elements negative 10 negative 5 0 5 and
53.699 -> 10 next we'll create a callback function
57.149 -> called greater than 0 that will be used
59.699 -> by the fine index method
64.979 -> it takes one parameter called t OT n
67.439 -> element it is within this function that
70.5 -> we will test whether t OT n element is
72.75 -> greater than 0
76.08 -> well write the output of the fine index
78.42 -> method to the console.log
82.35 -> we will then invoke the fine index
84.72 -> method of the t OT an array variable to
87.36 -> test each element value using the
89.61 -> greater than zero callback function
93.78 -> now let's execute our javascript code to
96.27 -> see what results we get in this example
99.21 -> the find index method has returned the
101.46 -> value three since the first element in
104.4 -> the array has an index of zero a value
107.729 -> of three represents the fourth element
109.77 -> in the array the value at index three of
113.04 -> the array is five and is the first value
115.74 -> that meets our criteria of being greater
117.84 -> than zero
120.28 -> next let's look at one more example that
122.77 -> uses the optional parameter called this
124.96 -> parameter in this example we will
127.63 -> declare an array object called t OT an
129.97 -> array that has four elements tek on the
134.11 -> end net
138.55 -> but this time we will also declare a
140.89 -> second variable with a string value of
143.17 -> the element being tested is we'll use
147.55 -> this variable as the this parameter when
150.25 -> we invoke the fine index method next we
153.73 -> will create a callback function called
155.74 -> contains and char that will be used by
158.26 -> the find index method similar to the
161.38 -> previous example it takes one parameter
163.33 -> called t OT n element within the
166.93 -> callback function we'll write the value
168.7 -> of this variable followed by the value
172.27 -> of t OT n element
176.43 -> we will discuss this variable later in
179.069 -> this tutorial
180.319 -> next we will test whether the T OTN
183 -> element contains the letter n to do this
186.209 -> we will use the string method called
187.89 -> index of index of returns the location
191.939 -> of a substring within a string and if it
194.519 -> doesn't find a match it will return
196.29 -> negative one
199.64 -> and finally we'll call the find index
202.1 -> method on the T OTN array
206.91 -> providing the callback function as the
209.38 -> first parameter and to this parameter
212.71 -> variable as the second parameter the
216.04 -> results of the find index method will be
217.99 -> displayed in the console log it is
220.42 -> important to note that that this
222.1 -> parameter value will be made available
223.75 -> within the callback function as the this
226.51 -> variable now let's see what our
229.3 -> JavaScript code returns since the first
232.57 -> element in the T OTN array is tech we
235.48 -> will see the message the element being
237.4 -> tested is tech displayed in the console
239.68 -> log tech does not contain the letter n
242.47 -> so the callback function evaluates to
244.63 -> false and the next element is tested
247.47 -> when the second element which has a
250.06 -> value of on is evaluated by the callback
252.61 -> function the letter n is found since on
256.45 -> contains the letter n the callback
258.34 -> function will return true since the
261.13 -> callback function returned true find
263.11 -> index will not process the remaining
265.21 -> elements and will instead return the
267.58 -> current element index which in this case
270.22 -> will be 1 this is why we will see a 1 in
273.46 -> the console log output
275.93 -> this covers a few basic examples of how
278.69 -> to use the fine index array method in
280.699 -> JavaScript if you found our tutorial
284 -> helpful please leave a like on this
285.8 -> video and don't forget to subscribe to
287.9 -> our YouTube channel for more great
289.61 -> JavaScript tutorials
Source: https://www.youtube.com/watch?v=G6dgJwommYQ