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 js array

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the common methods of js array what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe you read this js array of common methods of which articles will have a harvest, let's take a look at it.

The common methods of arrays are as follows

Method interpretation

Push ()

The push () argument method can take any number of parameters and add them to the end of the array, returning the latest degree of the array.

Unshift ()

Unshift () adds as many values as you want at the beginning of the array and returns the new array degree.

Splice ()

Pass three parameters, namely, the start position, 0 (the number of elements to be deleted), the inserted element, and return an empty array

Concat ()

"will first create a copy of the current array, then add its parameters to the end of the copy, and finally return the newly built array without affecting the original array."

Pop ()

The pop () method deletes the last item of the array, reduces the length value of the array, and returns the deleted item.

Shift ()

The shift () method deletes the first item of the array, reduces the length value of the array, and returns the deleted item.

Splice ()

Pass two parameters, which are the start position, the number of deleted elements, and return the array containing deleted elements

Slice ()

Slice () creates a new array containing one or more elements of the original array without affecting the original array.

IndexOf ()

Returns the position of the element you are looking for in the array, or-1 if it is not found

Includes ()

Return the location of the element you want to find in the array, find and return true, otherwise false

Find ()

Returns the first matching element

Two sorting methods of array

Reverse () inverts the array

Let values = [1,2,3,4,5]

Values.reverse ()

Alert (values); / / 5, 4, 4, 3, 2, 1.

Sort ()

The sort () comparison method accepts a comparison function to determine which value should be at the top of the list

Basic operation of array method

Increase

The basic operation of the array can be summarized as adding, deleting, changing and querying. It is necessary to pay attention to which methods will affect the production of the original array and which will not.

Push ()

Let colors = []; / / create an array

Let count = colors.push ("red", "green"); / / two items are recommended

Console.log (count) / / 2

Unshift ()

Let colors = new Array (); / / create an array

Let count = colors.unshift ("red", "green"); / / deduce two items from the beginning of the array

Alert (count); / / 2

Splice ()

Let colors = ["red", "green", "blue"]

Let removed = colors.splice (1,0, "yellow", "orange")

Console.log (colors) / / red,yellow,orange,green,blue

Console.log (removed) / / []

Concat ()

Let colors = ["red", "green", "blue"]

Let colors2 = colors.concat ("yellow", ["black", "brown"])

Console.log (colors); / / ["red", "green", "blue"]

Console.log (colors2); / / ["red", "green", "blue", "yellow", "black"

"brown"]

Delete

Pop ()

Let colors = ["red", "green"]

Let item = colors.pop (); / / get the last entry

Console.log (item) / / green

Console.log (colors.length) / / 1

Shift ()

Let colors = ["red", "green"]

Let item = colors.shift (); / / get item

Console.log (item) / / red

Console.log (colors.length) / / 1

Splice ()

Let colors = ["red", "green", "blue"]

Let removed = colors.splice (0jue 1); / / Delete item

Console.log (colors); / / green,blue

Console.log (removed); / / red, an array of only two elements

1234

Slice ()

Let colors = ["red", "green", "blue", "yellow", "purple"]

Let colors2 = colors.slice (1)

Let colors3 = colors.slice (1,4)

Console.log (colors) / / red,green,blue,yellow,purple

Concole.log (colors2); / / green,blue,yellow,purple

Concole.log (colors3); / / green,blue,yellow

Change

Splice ()

Let colors = ["red", "green", "blue"]

Let removed = colors.splice (1,1, "red", "purple"); / / insert two values and delete each element

Console.log (colors); / / red,red,purple,blue

Console.log (removed); / / green, an array of only two elements

Check

IndexOf ()

Let numbers = [1,2,3,4,5,4,3,2,1]

Numbers.indexOf (4) / / 3

Includes ()

Let numbers = [1,2,3,4,5,4,3,2,1]

Numbers.includes (4) / / true

Find ()

Const people = [

{

Name: "Matt"

Age: 27

}

{

Name: "Nicholas"

Age: 29

}

]

People.find ((element, index, array) = > element.age < 28) / {name:

"Matt", age: 27}

This is the end of the article on "what are the common methods of js arrays?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the common methods of js array". If you want to learn more, you are 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