Four methods of searching array elements in JavaScript

2021-10-14 11:16作者:admin来源:未知>次阅读

in actual development, we often encounter requirements such as the following:

get all items in the array that meet specific conditions. Do you want to check whether the conditions are met? Check whether there are specific values in the array? Find the index of the specified value in the array?

in this article, we will discuss four methods that can be used to search for items in an array. These methods are:

filterfindincludesindexof

next, let's take a look at these four methods

array. Filter()

we can use the array. Filter() method to find elements that meet specific conditions in the array.

for example, if we want to get all items in a number array greater than 10, we can do the following:

 const array = [10, 11, 3, 20, 5]; const greaterThanTen = array.filter(element => element > 10); Console.log (greaterthanten) / / [11, 20] 

the syntax of using array. Filter() method is as follows:

 let newarray = array. Filter (callback); 

here:

newarray is the returned new array. Array is the array we want to find. Callback is the callback function applied to each element of the array

. If no item in the array meets the conditions, an empty array is returned.

sometimes we don't need all elements that meet specific conditions. We only need one qualified element. In this case, you need to use the find () method.

Array. Find()

uses the array. Find() method to find the first element that meets a specific condition. Just like the filter method, it takes the callback as a parameter and returns the first element that meets the callback condition.

let's try using the find method on an array in the above example.

const array = [10, 11, 3, 20, 5]; const greaterThanTen = array.find(element => element > 10); The syntax of console.log (greaterthanten) / / 11 

array. Find() is

 let element = array. Find (callback); 

callback is a function executed on each value in the array with three parameters:

element - the currently traversed element (required) index - the index / location of the currently traversed element (optional) array - the current array (optional) JavaScript 中搜索数组元素的四种方法

but note that if no item in the group meets the criteria, it returns undefined.

but what if you want to check whether an element is in the array?

The array. Includes()

includes() method determines whether the array contains a value and returns true or false when appropriate.

therefore, in the above example, if we want to check whether 20 is one of the elements in the array, we can do the following:

 const array = [10, 11, 3, 20, 5]; const includesTwenty = array.includes(20); Console.log (includestwenty) / / true 

you will notice the difference between this method and other methods. This method accepts a value instead of a callback as a parameter. This is the syntax of the include method:

 const includesvalue = array. Includes (valuetofind, fromindex) 
valuetofind is the value to check in the array (required) fromindex is the index or position in the array from which to start searching for elements (optional)

to understand the concept of index, let's use the above example again.

if you want to check whether the array contains 10 elements other than the first element, you can do the following:

 const array = [10, 11, 3, 20, 5]; const includesTenTwice = array.includes(10, 1); The console.log (includestentwice) / / false 
array. Indexof()

indexof() method returns the first index of a given element that can be found in the array. If the element does not exist in the array, - 1 is returned.

back to the example. Let's find the index of 3 in the array.

const array = [10, 11, 3, 20, 5]; const indexOfThree = array.indexOf(3); The syntax of console.log (indexofthree) / / 2 

is similar to that of the includes method.

 const indexofelement = array.indexof (element, fromindex) 
element is the element to be checked in the array (required), and fromindex is the starting index or position of the element to be searched from the array (optional)

it is important to note that both the includes and indexof methods use strict equality (% 26#39; = = =% 26#39;) to search the array. If values are of different types (for example, 4 and% 26#39; 4% 26#39;), they return false and - 1, respectively.

Summary

uses these array methods to search arrays without using a for loop. Depending on your needs, you can decide which method is best for your use case.

the following is a summary of when to use each method:

if you want to find all items in an array that meet specific criteria, use filter. If you want to check whether at least one item meets specific conditions, use find. If you want to check that an array contains a specific value, use include. If you want to find the index of a specific item in an array, use indexof.

热门排行

最新文章

Powered by 世界滤芯新闻网 @2018 RSS地图 HTML地图

Copyright 365建站 © 2008-2021 华绿环保 版权所有