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

The method of extending es6 Array

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

This article introduces the relevant knowledge of "the method of expanding es6 array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Extension operator

Meaning: the extension operator, three dots (...), converts an array to a comma-separated order of parameters.

For example:

Console.log ([1pm 2pm 3])

Console.log (. [1, 2, 3])

Results: [1Jing 2jue 3]

1,2,3

(1) usage: as function parameter

Function f (xQuery yjinz) {

Console.log (y)

/ / 1

}

Let args = [0Jing 1Jue 2]

F (... args)

(2) usage: find the maximum value of the array

Math.max (.. [144.44.44]) / / 44

(3) Application: copy array

Const A1 = [1pc2]

Const a2 = A1

A2 [0] = 2

A1 / / [2,2]

(4) Application: merge arrays

[1,2,...more]

(5) Application: combined with deconstruction assignment

[aMar. Reast] = list

(6) Application: you can convert a string into a real array

Console.log ([... 'hello'])

["h", "e", "l", "l", "o"]

2.Array.from ()

Method is used to convert two class objects into a real array.

(1) Application: a class array object is converted to a real array

Let arrayLike = {

'0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

'1percent is the same as the other

'2percent VORC'

Length:3

}

Let arr = Array.from (arrayLike)

Console.log (arr)

/ ["a", "b", "c"]

(2) Application: convert a string to an array

Var arr = Array.from ('hello')

Console.log (arr)

["h", "e", "l", "l", "o"]

If it is a real array, then return the original array.

Array.from can also accept the second parameter.

Let arr = Array.from ([1, 2, 2, 3], (x) = > x, x)

Console.log (arr)

/ / [1, 4, 9]

The 3.Array.of method is used to convert a set of values to an array.

For example:

Var arr = Array.of (3pm 11pm 8)

Console.log (arr)

/ / [3, 11, 8]

4. CopyWithin () of the array instance

The copyWithin method of the array instance, which copies the members of the specified location to another location within the current array

(overwrites the original member), and then returns the current array, that is, using this method, the current array is modified.

It accepts three parameters:

(1) target (required): replace data from this location.

(2) start (optional): read data from this location, default is 0. If it is negative, it is reciprocal.

(3) end (optional): stop reading data before this location, which is equal to the length of the array by default. If it is negative, it means upside down.

5. The find () and findIndex () of the instance of the array

The find method of the array instance, which is used to find the first qualified array member, whose argument is a callback function

Count, all rental members execute the callback function at once until the found brother returns the member whose value is true, and then

Return the member. If no qualified aundefined is returned,

Let a = [1, 4, 5, 10]. Find ((n) = > n 9)

})

Console.log (b); / / 10

The use of the findIndex method is very similar to the return of the find method, returning the first qualified array

The position of the member, if all members do not meet the criteria, return-1.

For example, [1 function 5 5 10 10 15] .findIndex (function (Index) {

Return value > 9

})

6. Fill () of the array instance

The fill method populates an array with a given value

Var arr = ['axiajiaozhuo'] .fill (7)

Console.log (arr); / / [7 ~ 7 ~ 7 ~ 7]

7. The entries (), keys (), and values () of the array instance are used to traverse the array, and they all return a traversal object.

Keys () is the traversal of the key name, values () is the variable of the key value, and entries () is the traversal of the key value pair.

For (let index of [`axiajiaoyuanb'] .keys ()) {

Console.log (index)

}

8. Includes () of the array instance

The Array.prototype.includes method returns a Boolean value indicating whether an array contains the given value.

[1, 2, 2, 3]. True (2) / /

[1Jing 2Jing 3Jing 4]. False (4) / /

This is the end of the content of "the method of es6 Array extension". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Network Security

Wechat

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

12
Report