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 array Array in JavaScript

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what are the common methods of array Array in JavaScript", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn "what are the common methods of array Array in JavaScript".

I. how to create an array in JavaScript

(1) use the Array constructor:

Var arr1 = new Array (); / / create an empty array var arr2 = new Array (10); / / create an array of 20 items var arr3 = new Array ("zs", "ls", "ww"); / / create an array of 3 strings

(2) use array literal representation:

Var arr4 = []; / / create an empty array var arr5 = [10]; / / create an array containing 1 item var arr6 = ["zs", "ls", "ww"]; / / create an array containing 3 strings.

Method name function whether the original array changes join () to use the delimiter, converts the array to a string and returns npop () to delete the last bit, and returns the deleted data yshift () to delete the first bit, and returns the deleted data ypush to add one or more data at the end, return length yunshift () to add one or more data in the first place, and return length yconcat () merge array And return the merged data nslice () to intercept the array at the specified position, and return nsort () sort (character rules), return the result yreverse () to reverse the array, return the result ytoString () directly to a string, and return nsplice () to delete the specified position and replace Return deleted data yvalueOf () returns the original value of the array object nindexOf () query and returns the index of the data nlastIndexOf () reverse query and return the data index nforEach () parameter is a callback function, which traverses all items in the array, and the callback function accepts three parameters, namely value,index,self ForEach does not return the same value nmap () as forEach. At the same time, the callback function returns data, which forms a new array. Map returns nfilter () and forEach, and callback function returns Boolean value. For true data, filter returns nevery () and forEach, while callback function returns Boolean value, all true, truensome () same as forEach, and callback function returns Boolean value. As long as one is true, some returns truenreduce () and merges with forEach. Iterate over all the items of the array and build a final value. Reduce returns nreduceRight () to reverse merge, iterate all items of the array with forEach, and build a final value, and reduceRight returns nfindIndex ()

The subscript that finds the first eligible element in the array is no longer traversed

N III. Detailed explanation of the method.

1.join ()

Function: places all elements in the array into a string according to the specified delimiter and returns the string.

Parameter: join (str); optional, default is the "," sign, with the passed character as the delimiter.

Var arr = [1Jing 2 console.log 3]; console.log (arr.join ()); / / 1 Jol 2 console.log ("-"); / / 1-2-3

You can implement the repeating string through the join () method. You only need to pass in the string and the number of repeats, and the repeated string can be returned. The function is as follows:

Function repeatString (str, n) {return new Array (n + 1) .join (str);} console.log (repeatString ("abc", 3)); / / abcabcabcconsole.log (repeatString ("Hi", 5)); / / HiHiHiHiHi2.pop ()

Pop (): removes the last item at the end of the array, reduces the length value of the array, and then returns the removed item

Ar arr = [1Jing 2jue 3]; console.log (arr.pop ()); / / 3console.log (arr); / / [1Jing 2]-the original array changes 3.shift ()

Function: the method is used to delete and return the first element of the array.

Var arr = [1Jing 2jue 3] console.log (arr.shift ()); / / 1console.log (arr); / / [2Jue 3]-the original array changes 4.push ()

Function: adds one or more elements to the end of the array and returns a new length.

Var arr = [1Jing 2jue 3]; console.log (arr.push ("hello")); / / 4console.log (arr); / / [1Yui 2je 3, "hello"]-the original array changes console.log (arr.push ("a", "b")); / / 6console.log (arr); / / [1Jing 2Jing 3, "hello", "a", "b"]-- the original array changes 5.unshift ()

Function: adds one or more elements to the beginning of the array and returns a new length.

Var arr = [1Jing 2jue 3]; console.log (arr.unshift ("hello")); / / 4console.log (arr); / / ["hello", 1je 2jue 3]-the original array changes console.log (arr.unshift ("a", "b")); / / 6console.log (arr); / / ["a", "b", "hello", 1pm 2p3]-the original array changes 6.concat ()

Function: add parameters to the original array. This method first creates a copy of the current array, then adds the received parameters to the end of the copy, and finally returns the newly built array. Without passing arguments to the concat () method, it simply copies the current array and returns a copy.

Const arr1 = [1Jing 2 Jue 3]; const arr2 = [4 Jet 5 Jol 6]; const arr3 = arr1.concat (arr2); console.log (arr3); / / [1 Jing 2 Jing 3 Jing 4 5 Jing 6] 7.slice ()

Function: returns a new array of items between the starting subscript and the ending subscript specified in the original array.

The slice () method can accept one or two parameters, that is, the start and end positions of the item to be returned. In the case of only one parameter, the slice () method returns all items from the specified position of that parameter to the end of the current array. If there are two parameters, the method returns the item between the start and end position-- but does not include the item at the end position.

Var arr = [1meme 3jorie 5ju 7jue 9ju 11]; var arrCopy1 = arr.slice (1); var arrCopy2 = arr.slice (1pas 4); var arrCopy3 = arr.slice (1mai Leim 2); var arrCopy4 = arr.slice (- 4m Linda1); console.log (arr); / / [1m 3m 5m 7e 7m 9J 11] (original array unchanged) console.log (arrCopy1); / [3pr 5e 7e 9m 11] console.log (arrCopy2) / / [3,5,7] console.log (arrCopy3); / / [3,5,7] console.log (arrCopy4); / / [5,7,9] 8.sort ()

Function: sort the elements in the array. The default is ascending order.

Var arr = [6Jing 1je 5jue 2jue 3]; console.log (arr.sort ()); / / [1,2pint 3,5,6] console.log (arr); / / [1,2pint 3,5,6]-the original array changes 9.reverse ()

Function: reverses the order of array items.

Var arr = [13,24,51,3]; console.log (arr.reverse ()); / / [3,51,24,13] console.log (arr); / / [3,51,24,13] (original array change) 10.toString ()

Function: convert to a string, similar to join () with no parameters. This method is called automatically when the data is implicitly typed or, if manually, converted directly to a string.

Var arr = [1Jing 2Jue 3]; console.log (arr.toString ()); / / 1Jet 2meme 3console.log (arr); / / [1Jing 2Jue 3]-the original array has not changed 11.splice ()

Function: a powerful array method that can be used in many ways to delete, insert, and replace.

Delete: you can delete any number of items by specifying two parameters: the location of the first item to be deleted and the number of items to be deleted. For example, splice (0Pol 2) deletes the first two items in the array.

Insert: you can insert any number of items into a specified location by providing three parameters: the starting position, 0 (the number of items to delete), and the item to be inserted. For example, splice (2, 0, 4, 6) inserts 4 and 6 starting at position 2 of the current array.

Replace: you can insert any number of items into a specified location and delete any number of items at the same time, just specify three parameters: the starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted. For example, splice deletes the item in position 2 of the current array, and then inserts 4 and 6 starting at position 2.

The splice () method always returns an array containing items deleted from the original array, or an empty array if no items are deleted.

Var arr = [1meme 3, 5Power9]; var arrRemoved = arr.splice (0meme 2); console.log (arr); / / [5mie 7 pint 9 pint 11] console.log (arrRemoved); / / [1m 3] var arrRemoved2 = arr.splice (2m 0pas 4); console.log (arr); / / [5m 7,4e 6,9 min 11] console.log (arrRemoved2); / / [] var arrRemoved3 = arr.splice (1m 1m 2); console.log (arr) / [5, 2, 4, 4, 6, 9, 11] console.log (arrRemoved3); / / [7] 12.valueOf ()

Function: returns the original value of the array (usually the array itself), which is usually called by js in the background and does not appear explicitly in the code

Var arr = [1je 2jue 3]; console.log (arr.valueOf ()); / / [1je 2je 3] console.log (arr); / / [1je 2jue 3] / / to prove that the array itself console.log (arr.valueOf () = = arr) is returned; / / true13.indexOf ()

Function: according to the specified data, from left to right, query the location in the array, if there is no specified data, return-1. This method is a query method and does not change the array.

Parameter: indexOf (value, start); value is the data to be queried; start is optional, indicating the location to start the query. If start is negative, it is counted forward from the end of the array; if the query cannot find the existence of value, the method returns-1

Var arr = ["h", "e", "l", "l", "o"]; console.log (arr.indexOf ("l")); / / 2console.log (arr.indexOf ("l", 3)); / / 3console.log (arr.indexOf ("l", 4)); / /-1console.log (arr.indexOf ("l",-1)); / /-1console.log (arr.indexOf ("l",-3)) / / 214.lastIndexOf ()

Function: according to the specified data, from right to left, query the location in the array, if there is no specified data, return-1. This method is a query method and does not change the array.

Parameter: lastIndexOf (value, start); value is the data to be queried; start is optional, indicating the location to start the query. If start is negative, it is counted forward from the end of the array; if the query cannot find the existence of value, the method returns-1

Var arr = ["h", "e", "l", "l", "o"]; console.log (arr.lastIndexOf ("l")); / / 3console.log (arr.lastIndexOf ("l", 3)); / / 3console.log (arr.lastIndexOf ("l", 1)); / /-1console.log (arr.lastIndexOf ("l",-3)); / / 2console.log (arr.lastIndexOf ("l",-4)) / /-115.forEach ()

Function: iterate through the array and run a given function on each item in the array. This method does not return a value.

Parameters: all are of function type. Parameters are passed by default. The parameters are the contents of the array traversed, the corresponding array index, and the array itself.

Var arr = [1,2,3,4,5]; arr.forEach (function (x, index, a) {console.log (x +'|'+ index +'|'+ (a = arr)); / / output is: / / 1 | 0 | true// 2 | 1 | true// 3 | 2 | true// 4 | 3 | true// 5 | 4 | true16.map ()

Function: runs a given function on each item in the array and returns an array of results of each function call.

Use map if you want every value in the array to change

Let arr = [10,30,50,60,120,230,340,450] let newArr = arr.map (n = > {return n * 2}) console.log (newArr); 17.filter ()

Function: filter, each item in the array runs a given function and returns an array that meets the filtering criteria.

Var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var arr2 = arr.filter (function (x, index) {return index% 3 = 0 | | x > = 8;}); console.log (arr2); / / [1,4,7,8,9,10] 18.every ()

Function: determine whether each item in the array meets the condition, and true will be returned only if all items meet the condition.

Var arr = [1,2,3,4,5]; var arr2 = arr.every (function (x) {return x)

< 10;}); console.log(arr2); //truevar arr3 = arr.every(function(x) {return x < 3;}); console.log(arr3); // false19.some() 功能: 判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。 var arr = [1, 2, 3, 4, 5];var arr2 = arr.some(function(x) {return x < 3;}); console.log(arr2); //truevar arr3 = arr.some(function(x) {return x < 1;}); console.log(arr3); // false20.reduce() 功能:从数组的第一项开始,逐个遍历到最后,迭代数组的所有项,然后构建一个最终返回的值。 参数: 第一个参数是:accumulator是当前聚合值, 第二个参数是: current是数组循环时的当前元素 第三个参数是: index 是数组元素的索引值 第四个参数是: Array 是数组本身 int : 是accumulator的初始值 可以自行进行设置 一般常用的是前面的两个参数,后面两个参数不常用,常用的使用场景便是数组的求和 // 作用:对数组中所有的内容进行汇总 要传至少两个值 let arr = [10, 30, 50, 60, 120, 230, 340, 450] let newArr = arr.reduce((pre, n) =>

{return pre + n}, 0) console.log (newArr); 21.reduceRight ()

Function: (similar to reduce) start with the last item of the array, iterate through the first bit one by one, iterate over all the items in the array, and then build a final returned value.

Parameter: same as reduce.

Var arr = [1Jing 2 Jing 3 Jing 4 Jing 5]; var sum = arr.reduceRight (function (pre, cur, index, array) {return pre + cur;}, 10); console.log (sum); / / 2522.findIndex ()

Function: returns the index of the first element in the array that satisfies the provided test function. If not, return-1.

Let arr = [10, 2, 9, 17, 22]; let index = arr.findIndex ((item) = > item > 13) console.log (index); / / 3 are all the contents of the article "what are the common methods of array Array in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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