Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of JavaScript array

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the JavaScript array which commonly used methods, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Preface

There are too many array methods. This article records some array methods that are easy to be misremembered for consolidation and review.

Other array methods will be slowly added later.

Making good use of array methods can make data processing elegant and simple.

So let's get started:

2. Filter ()

Description:

The filter () method creates a new array and adds all the elements that meet the criteria to the new array created.

Syntax:

Array.filter (callback (element, index, array) {/ / function body}, thisValue)

Parameters:

Callback and thisValue:

Whether the parameter is optional describes the function of the callback parameter that must be selected to test each element of the array. Returning true means that the element passes the test, keeping the element and not false. It takes three parameters. The parameter two thisValue is an optional value for this when callback is executed.

Object is used as the callback for the execution, passed to the function.

If thisValue is omitted, the value of "this" is "undefined"

Parameter list of callback:

Whether the parameter is optional description parameter one element must be selected current element parameter two index optional index value parameter three array optional call to filter's array itself

Parameters of thisValue:

The value used for this when callback is executed.

Return value:

Is a new array consisting of the return value of the filter () parameter-function

Example:

Let users = [{id: 11, name: "Sun WuKong"}, {id: 21, name: "Zhu Bajie"}, {id: 31, name: "Sha Wujing"}]; / / returns the array let filterUsers = users.filter (item = > item.id) of the first two users

< 31);console.log(filterUsers.length); // 23、map() 描述: map() 方法创建一个新数组,新数组的每个元素是调用一次给定函数的返回值。 语法: Array.map(callback(currentValue, index, array) { // 函数体 }, thisValue) 参数: callback与thisValue 参数是否可选描述参数一callback必选数组中的每个元素都要运行的回调函数。它接收三个参数。参数二thisValue可选执行 callback 时,用于 this 的值。 对象作为该执行回调时使用,传递给函数。 如果省略了 thisValue ,"this" 的值为 "undefined" callback的参数列表: 参数是否可选描述参数一element必选当前元素参数二index可选当前元素的索引值参数三array可选调用了 filter 的数组本身 thisValue的参数: 执行 callback 时,用于 this 的值。 返回值: 是一个由原数组每个元素执行回调函数的结果组成的新数组。 示例: let number = [1, 2, 3].map(item =>

Item + 1); console.log (lengths); / / 2,3,44, sort ()

Description:

The map () method modifies the order of the elements on the original array

Note: the default sort order is when you convert elements to strings and then compare their UTF-16 code unit value sequences

Syntax:

Array.sort (compareFunction)

Parameters:

CompareFunction

Whether the parameter is optional describes the parameter-compareFunction optional function that specifies the order of arrangement. If omitted, the elements are sorted by the Unicode sites of each character of the converted string.

Parameter list of compareFunction:

Whether the parameter is optional or not the first element used for comparison is required for firstEl. Parameter two secondEl must select the second element for comparison.

CompareFunction return value

Returns a number that describes the order of the two values

If an is less than b and a should appear before b in the sorted array, a value less than 0 is returned.

If an equals b, 0 is returned.

If an is greater than b, a value greater than 0 is returned.

Return value:

The return value is the sorted array, but the return value is usually ignored because the original array has been modified.

Example:

Let number = [1,2,3] .map (item = > item + 1); console.log (lengths); / / 2,3,45, reduce ()

Description:

The reduce () method executes (ascending order execution) callback functions for each element in the array in turn, excluding elements in the array that have been deleted or never assigned. The result is calculated as a value.

Note: reduce () can be used as a higher-order function for the compose of a function.

Syntax:

Array.reduce (accumulator, currentValue, index, array), initialValue)

Parameters:

Callback and initialValue

Whether the parameter is optional-callback is required to execute the function of each array element in the array (if there is no initialValue initial value then the first value will not execute the change function) it receives four parameters. Parameter 2 initialValue optional initial value of callback function

Parameter list of callback:

Whether the parameter is optional describes the return value of the cumulative callback of the accumulator required by the accumulator; it is the cumulative value returned when the callback was last called, or initialValue. Parameter 2 currentValue must be selected current element parameter 3 index optional index value parameter 4 array optionally calls the array itself of reduce ()

Parameters of initialValue:

As the value of the first parameter when the callback function is called for the first time. If no initial value is provided, the first element in the array is used as the initial value. Calling reduce on an empty array with no initial value will report an error.

Return value:

The result of the cumulative processing of the function

After executing the accumulator parameters of all callback functions

Note: the return value of our reducer function is assigned to the accumulator, which is remembered in each iteration of the array and ends up as a single result value.

Example:

Let number = [1,2,3,4]; let result = number.reduce ((sum, current) = > sum + current, 0); console.log (result); / / 106, forEach ()

Description:

The reduce () method executes (ascending order execution) callback functions for each element in the array in turn, excluding elements in the array that have been deleted or never assigned. The result is calculated as a value.

Note: reduce () can be used as a higher-order function for the compose of a function.

Syntax:

Array.forEach (callback (currentValue, index, array), thisValue)

Parameters:

Callback and thisValue

Whether the parameter is optional describes the parameter-callback must execute the function of each array element in the array, which receives three parameters. The parameter two thisValue is an optional value for this when callback is executed.

Object is used as the callback for the execution, passed to the function.

If thisValue is omitted, the value of "this" is "undefined"

Parameter list of callback:

Whether the parameter is optional description parameter 1 currentValue is required current element parameter 2 index optional index value parameter 3 array optional invokes the array itself of forEach ()

Parameters of thisValue:

The value used for this when callback is executed.

Return value:

The method does not return a value.

Example:

Let number = [1, 2, 3, 4]; number.forEach ((item, index, array) = > {console.log (item); / / 1ax 2 + 3 + 4}); 7. List of methods

Method properties:

Method to change the return value description of the original array to describe the new array filter after filter () filter map () No loop the new array loop sort () is to sort the sorted array reduce () is the cumulative result of the function accumulator forEach () is no return value as undefined loop

Thank you for reading this article carefully. I hope the article "what are the common methods of JavaScript array" shared by the editor will be helpful to you? at the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report