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 commonly used methods and techniques in JS array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "what are the commonly used methods and skills in the JS array", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "what are the commonly used methods and skills in the JS array"!

Catalogue

Splice () method

Join () method

Reverse () method

Every () method

Reduce () method

Filter () method

FindIndex () method and find () method

FindIndex ()

Find ()

ForEach () method

Some () method

IndexOf () method

Sort () method

Push () method

Pop () method

Unshift () method

Shift () method

Splice () method

Intercept replacement array

The first parameter is the starting position, the second is the number of intercepts, the third is the replacement element, the return value is the intercepted element, and the original array is the remaining element.

Join () method

Array to string

What is the connection in ()? if you enter the empty string join (''), it will be directly spliced and undivided.

For example:

Let aaa = [1-2-3] let bbb = aaa.join ('-') let ccc = aaa.join ('0') console.log (bbb) / /'1-2-3'console.log (ccc) / / '10203'reverse () method

Flip array

Arr.reverse () flips the array. The return value is the flipped array.

Every () method

To find an array that does not meet the criteria

If there is an item in the traversal detection array that does not meet the condition you defined, the rest will not be executed. Only if each item meets the condition, it will return true.

The first parameter is each item of the array, the second is the index, and the third is the current array.

Example:

Arr: [121 return item 3322, 3215 3549, 6899 46456] arr.every (function (item,index,array) {return item > 2000 / / check whether each value of the array is greater than 2000}) / / the result is false unless each value of the array is greater than 2000 is the truereduce () method

Find the cumulative value

The result of traversing the previous array items can be calculated with the current traversal item.

The first parameter is the accumulator (save the result returned by the second value) prev

The second value is the value currently being processed (traversing the array from beginning to end) cur

The third index index

The fourth current array arr

The fifth initial value (after the function) init

Example:

Var arr = [3jingjie 4jue 3jue 6j0jue 9]; var sum = arr.reduce (function (prev, cur) {return prev + cur;}, 0); / / because the initial value is 0, that is, the initial value of prev is 0, so the calculation is 0, 3, 3, 9, 12, 12, 4, 16. Each time the result is stored in prev for the next operation, which is the simplest reduce summation filter () method.

Traverse and filter the array

The first parameter is each item of the array, the second is the index, and the third is the current array.

It traverses the array to filter your defined conditions according to the criteria you define and returns a new array containing all the elements that meet the criteria.

Example:

Var arr=] var sum = arr.filter (function (value,index,arr) {return value > 3 / / filter elements greater than 3 in arr values}) console.log (sum) / / the returned values are [5recover7 sum] findIndex () method and find () method findIndex ()

Find out the index of the first qualified array member and cannot return-1

For an empty array, the function does not execute and does not change the original value of the array.

Find ()

The find () function is used to find the target element. If it is found, it will be returned, but the undefined will not be returned.

The lookup function has three parameters:

Value: the array elements found at each iteration.

Index: the index of array elements found at each iteration.

Arr: the array to be looked up.

ForEach () method

Traversing circular array

The first value is each parameter

The second value is the index

The third is the array itself.

Often used to traverse elements in an array

Arr: [1meme 2jue 3] arr.forEach (function (item,index,array) {console.log (item) / / 1meme 2mer3}) some () method

Check whether the element in the array satisfies the condition, which is used to find the unique value that returns true or false

Var a = arr.some (function (item,index,array) {return item > 3 / / detects whether there are elements greater than 3, returns true if not, returns false} if not)

As long as you find an element that satisfies the condition, the loop is terminated. If you encounter return trun in some, the loop will be terminated.

IndexOf () method

Finds whether an element exists in the array and returns the subscript. Returns the first index in the array where a given element can be found, or-1 if it does not exist.

Parameters.

First parameter (must): the element to be found

The second (optional): start the search position (cannot be greater than or equal to the length of the array, return-1), accept a negative value, the default value is 0.

Strictly equal search:

The indexOf search of an array is different from the indexOf of a string. The indexOf of an array uses strict equality = = to search for elements, that is, the array elements have to match exactly for the search to be successful.

Sort () method

Optional parameters: a comparison function that specifies the sort order.

By default, if the sort () method does not pass a comparison function, it defaults to ascending alphabetical order, and if the element is not a string, it will call the toString () method to convert the element to the Unicode (universal code) point of the string, and then compare the characters.

/ / the first letters of the string are var a = ["Banana", "Orange", "Apple", "Mango"]; a.sort (); / ["Apple", "Banana", "Mango", "Orange"] / / when the numbers are converted to Unicode strings, some numbers will be compared at the back of var a = [10,1,3,20 pens 25pc8]; console.log (a.sort ()) / / [1meme 10, 20, 25, 25, 8] Push () method

Push adds a new element to the end of the array (multiple elements can be added at once)

Return value: length (length) of the new array

Const aa = [1Jing 2 Jing 3] aa.push (5 Jing 6) console.log (aa) / / [1 Jing 2 Jing 3 Jing 5 Jing 6] pop () method

The tail deletes an element whose return value is deleted.

Unshift () method

The header is added, and the return value is the array length.

Shift () method

Header delete element return value: deleted element

Thank you for your reading, these are the contents of "what are the methods and techniques commonly used in the JS array". After the study of this article, I believe you have a deeper understanding of the methods and skills commonly used in the JS array, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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