Home >Web Front-end >JS Tutorial >What is the traversal method of array in js
Array traversal methods in JavaScript include: forEach(): Execute the specified function for each element. map(): Creates a new array containing the results of applying the specified function. filter(): Creates a new array containing elements that meet the specified conditions. find(): Returns the first element that meets the specified conditions. findIndex(): Returns the index of the first element that meets the specified condition. some(): Checks whether there is at least one element that meets the specified condition. every(): Checks whether all elements meet the specified conditions. reduce(): Reduce the array to a cumulative value.
Array traversal method
Array traversal refers to the process of sequentially accessing and operating each element in an array. JavaScript provides several methods for traversing arrays.
forEach() method
forEach()
method executes the specified function for each element in the array. The syntax is:
<code>array.forEach((element, index, array) => { // 对每个元素执行操作 });</code>
where:
element
is the element currently being processed. index
is the index of the current element in the array. array
is a reference to the original array. map() method
map()
method creates a new array containing the The result after specifying the function. The syntax is:
<code>const newArray = array.map((element, index, array) => { // 对每个元素执行操作并返回新值 });</code>
filter() method
filter()
method creates a new array containing the original elements in the array. The syntax is:
<code>const newArray = array.filter((element, index, array) => { // 返回 true 以保留元素,返回 false 以移除元素 });</code>
find() method
find()
The method will return the first element in the original array that satisfies the specified condition. element. If no element is found, undefined
is returned. The syntax is:
<code>const element = array.find((element, index, array) => { // 返回 true 以返回找到的元素 });</code>
findIndex() method
findIndex()
method returns the index of the first element in the original array that satisfies the specified condition. index. If no elements are found, -1
is returned. The syntax is:
<code>const index = array.findIndex((element, index, array) => { // 返回 true 以返回找到的元素的索引 });</code>
some() method
some()
method checks whether at least one element in the original array meets the specified condition . The syntax is:
<code>const result = array.some((element, index, array) => { // 返回 true 以停止检查并返回 true,返回 false 以继续检查 });</code>
every() method
every()
method checks whether all elements in the original array meet the specified conditions . The syntax is:
<code>const result = array.every((element, index, array) => { // 返回 false 以停止检查并返回 false,返回 true 以继续检查 });</code>
reduce() method
reduce()
method reduces the original array to a cumulative value. The syntax is:
<code>const accumulator = array.reduce((accumulator, element, index, array) => { // 返回新的累积值 }, initialValue);</code>
where:
accumulator
is the current cumulative value. initialValue
is an optional initial cumulative value, if not specified, the first element in the array is used. The above is the detailed content of What is the traversal method of array in js. For more information, please follow other related articles on the PHP Chinese website!