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-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the commonly used methods of JavaScript array". In daily operation, I believe that many people have doubts about the common methods of JavaScript array. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the common methods of JavaScript array?" Next, please follow the editor to study!

Var arr = ["a", "b", "c"]

Arr.push ('d')

Console.log (arr); / / ["a", "b", "c", "d"]

/ / add a d to the end of the array and return the newly added value.

/ / so you don't need to assign a value again when you use it.

Arr.pop () / / removes the last value in the array.

/ / return the value after removal

Console.log (arr); / / ["a", "b", "c"]

Arr.unshift (); / / add one or more values to the front of the array. The usage is similar to push.

Arr.shift (); / / removes the first element in the array. Similar to pop.

/ / =

/ / arr.splice (); it means to delete and modify. This method sorts in the table

/ / or when so-and-so moves up and down, you can use this method to solve it.

/ / arr.splice ('initial subscript of deleted element', 'number of deletions')

Var arr1 = ["a", "b", "c"]

Arr1.splice (0,1); / / Delete the first element, delete a

Console.log (arr1); / / ["b", "c"]

/ / so there are ways to delete the last element.

Var arr2 = ["a", "b", "c"]

Arr2.splice (arr2.length-1,1)

Console.log (arr2); / / ["a", "b"]

/ / arr3.splice ('starting subscript', the number of deleted elements, and "multiple elements can be inserted")

Var arr3 = ["a", "b", "c"]

Arr3.splice (1, 0, "ke"); / / ["a", "ke", "b", "c"]

Console.log (arr3)

/ / arr4.slice (aQuery b); query the value between the subscript starting with an and ending with b. [a _ dint _ b)

Var arr4 = ['averse,' baked, 'crested,' d']

Var aa = arr4.slice (1,3)

Console.log (aa); / / ["b", "c"]

/ / arr5.join ('-') changes the array into a string and concatenates it with a "-" sign

Arr5 = ['averse,' baked, 'crested,' d']

Console.log (arr5.join ('-')) / / a-b-c-d

/ / arr6.reverse () upside down

Arr6 = ['averse,' baked, 'crested,' d']

Console.log (arr6.reverse ()); / / ["d", "c", "b", "a"]

/ / arr7.concat (arr8) joins two arrays together. If there are multiple, arr7.concat (arr8,arr9)

Arr7 = ['averse,' baked, 'crested,' d']

Arr8 = [1,23]

Var arr9 = arr7.concat (arr8)

Console.log (arr9); / / ["a", "b", "c", "d", 1, 23]

/ / arr10.sort is in ascending and descending order

Var arr10 = [10,20,30,1,3,6,22]

Arr10.sort ((a, b) = > {

Return Amurb; / / aMub is ascending order, Bmura is descending order.

})

Console.log (arr10)

/ / [1, 3, 6, 10, 20, 22, 30]

At this point, the study of "what are the common methods of JavaScript array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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