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

An example Analysis of the use of the Array of ES6 basic Syntax

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the ES6 basic grammar array of the use of case analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this ES6 basic grammar array of the use of case analysis article will have a harvest, let's take a look.

1. Array.of ()

Form an array of all the values in the parameter as elements:

Console.log (Array.of (1,2,3,4)); / / [1,2,3,4]

The values of parameters can be of different types:

Console.log (Array.of (1,'2, true)); / / [1,'2, true]

Returns an empty array when the parameter is empty:

Console.log (Array.of ()); / / []

Note:

Let arr1 = new Array (10); / / is an empty array let arr2 = Array.of (10) of length 10; / / Array II, Array.from () with length 1 and the first element value of 10

The argument is an array and returns the same array as the original array:

Console.log (Array.from ([1,2])); / / [1,2]

The parameter contains vacancies:

Console.log (Array.from ([1, 3]); / / [1, undefined, 3]

Array elements are processed to form a new array:

Let arr = Array.from ([1JI 2jue 3], n = > nasty 2); console.log (arr); / / [2Jing 4jue 6]

Use the function to process the array to form a new array (odd + 1, even constant rule):

/ / Plan 1: let arr1 = [1 if 2, 3, 4, 5, 5, 6]; let arr2 = Array.from (arr1,function (n) {if (n% 2), return, 1, else return n;}); console.log (arr2); / / option 2: function f (n) {if (n% 2), return, 1; else return n } let arr1 = [1, return 2, 3, 4, 5, 5, 6]; let arr2 = Array.from (arr1,function (n) {return f (n);}); console.log (arr2); III. Object conversion of class array

Convert an array-like object to a real array:

Let arr = Array.from ({0: "jack", 1: "rose", 2: "jordan", length: 3}); console.log (arr); / / ["jack", "rose", "jordan"]

If there is no length property, an empty array is returned:

Let arr = Array.from ({0: "jack", 1: "rose", 2: "jordan",}); console.log (arr); / / []

The element attribute name is not a numeric value and cannot be converted to a numeric value, and an array of length length element value undefined is returned:

Let arr = Array.from ({a: "jack", b: "rose", c: "jordan", length: 3}); console.log (arr); / / [undefined,undefined, undefined] IV. Transform iterable object

Convert map:

Let map = new Map (); map.set ('23rd Perry'); map.set ('335th' 'Pippen'); map.set ('995th' Rodman'); console.log (Array.from (map)); / / ['23rd' 'Jordan'], ['3358' Pippen'], ['995th' Rodman']

Convert set:

Let set = new Set (); set.add ("Jordan"); set.add ("Pippen"); set.add ("Rodman"); console.log (Array.from (set)); / / ["Jordan", "Pippen", "Rodman"]

Convert string:

Let str = "hello!"; console.log (Array.from (str)); / / ['hackers, girls, boys, girls, girls, Extension operator.

Copy the contents of the array:

/ / Scheme 1: copy the contents of the array / / let arr1 = [1, arr1, 2, 3, 4]; / / let arr2 = [... arr1]; / / console.log (arr2); / / console.log (arr1 = arr2); / / false (arr1 and arr2 references are different, but the values are the same) / / Scheme 2: direct array name assignment / / let arr1 = [1) let arr2 = arr1;// console.log (arr2); / / console.log (arr1 = arr2) / / true (the references of arr1 and arr2 are the same)

Merge arrays:

Let arr1 = [1 arr1,...arr2 2 3 arr1,...arr2 4]; let arr2 = [1 recorder 2 Japanese 3 4]; let arr = [. Arr1,...arr2]; console.log (arr)

As a function argument (you can accept arguments of variable length):

Function Add (... items) {let sum = 0; for (let item of items) {sum + = item;} return sum;} let result1 = Add; let result2 = Add; console.log (result1); console.log (result2); VI.

Find:

/ / find find () and findIndex () / / find (): find the eligible elements in the array, and return the first element if there are multiple eligible elements. / / let arr = ["Jordan", "Pippen", "Rodman"]; / / let r = arr.find (item= > item= = "Pippen"); / / console.log (r); / / Pippen / / let arr = ["Jordan", "Pippen", "Rodman"]; / / let r = arr.find (item= > item= = "Yao Ming"); / / console.log (r); / / undefined / / let arr = [1, 2, 3, 4] / / console.log (arr.find (item = > item > 2)); / / 3//findIndex (): find the index of eligible elements in the array. If there are multiple eligible elements, the first element index is returned. Let arr = [10, 20, 30, 40]; console.log (arr.findIndex (item = > item > 10)); / / 1

Fill:

/ / fill (): populates the contents of array elements indexed in a certain range to a single specified value. / / Parameter 1: value used to populate / / Parameter 2: populated start index / / Parameter 3 (optional): populated end index, defaults to the end of the array / / let arr = [1Jie 2jue 3re4]; / / arr.fill (0rec 1rect 2); / / console.log (arr); / / copyWithin (): modifies the array elements indexed in a certain range to the elements indexed in another specified range of this array. / / Parameter 1: modified starting index / / Parameter 2: the starting index of the data used to be overwritten / / Parameter 3 (optional): the end index of the data used to be overwritten, defaults to the end of the array / / let arr = [1jue 2ju 3jue 4]; / / arr.copyWithin (0meme 2pr 4); / / console.log (arr); / / [3pr 4,3pr 4] / / let arr = [1m 2,4] / / arr.copyWithin (0,2,4); / / console.log (arr); / / [, 4, 4] / / the first parameter is negative to indicate reciprocal / / let arr = [1, 2, 4]; / / arr.copyWithin (- 2, 0); / / console.log (arr); / / [1, 2, 1, 2]

Contains:

/ / includes (): whether the array contains the specified value. / / Parameter 1: specified value included / / let arr = [1jin2dag3]; / / let r = arr.includes (1); / / true// console.log (r); / / Parameter2: optional, starting index of the search, default is 0let arr = [1jing2jue 3]; let r = arr.includes (1,1); / / falseconsole.log (r)

Change a nested array to an one-dimensional array:

Console.log ([1, [2,3]. Flat ()); / [1,2,3] / / specify the number of nested layers of the transformation console.log ([1, [2, [3, [4,5] .flat (2)); / / [1,2,3, [4,5]] / / No matter how many layers of console.log are nested ([1, [2, [3, [4,5] .flat (Infinity)) / / [1, 2, 3, 4, 5] / / automatically skip vacant console.log ([1, [2, 3]] .flat ())

/ / [1, 2, 3] this is the end of the article on "case Analysis of the use of ES6 basic Grammar arrays". Thank you for reading! I believe you all have a certain understanding of the knowledge of "case Analysis of the use of ES6 basic Grammar 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: 237

*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