Expansion of arrays

Source: Internet
Author: User
Tags array length new set

1,array.from ()

Used to convert two types of objects to array objects, such as Class array objects and traversed objects (including ES6 new set and map structures)

1) Common array-like objects are nodelist objects and arguments objects inside functions returned by DOM operations.

NodeList Object

Let PS = Document.queryselectall (' P '); Array.from (PS). ForEach (function (p)) {  console.log (p);      }

Arguments Object

function foo () {  var args = array.from (arguments);   The extension operator can also be converted to the array var args = [... arguments]     //    ...  

An array-like object must have the length property. Therefore, any object that has a length property, Array.from can convert it to an array, such as Array.from ({length:3}) return [Undefined, undefined, Undefined

2) A Traversal object refers to an object that deploys a iterator interface, such as a string, set structure.

String

Array.from (' hello ')  //[' h ', ' e ', ' l ', ' l ', ' O ']

Set structure

Let NameSet = new Set ([' A ', ' B ']) Array.from (nameset)//[' A ', ' B ']

For browsers that have not yet deployed this method, you can use the Array.prototype.slice (ES5) method instead

Const ToArray = (() = Array.from? Array.from:obj = [].slice.call (obj)) ();

Array.from can also receive the second parameter, which is used to process each element, putting the processed value into the returned array

Array.from ([1,2,3],n=>n*n)//[1,4.9]

Array.from ([1,,2,,3],n = n| | 0)//[1,0,2,0,3] A member with a Boolean value of false is converted to 0

Another function of Array.from is to return the length of the string, avoiding a bug that jacascript a character greater than \uffff as a 2 character

function Countsymbols (str) {  return Array.from (str). Length  }

2,array.of ()

Used to convert a set of values to an array, to compensate for the lack of arrays (), because different parameters cause the behavior of array () to differ.

Array.of (3,11,8)//[3,11,8]arra.of (3)//[3]array.of (3). Length//1array ()//[]array (3)//[,,]array (3,11,8)//[3,11,8]

3, array instance of Copywithin ()

Inside the array, an unknown amount of the member is executed to another location (overwriting the original member), and then the current array is returned, and the pre-drop array is changed.

Array.prototype.copyWithin (target,start=0,end = this.length)

Target: Replace data from this location

Start: Reads data from this location. Default 0

End: Stops reading data before it is to that location. Default array length

[1,2,3,4,5].copywithin (0,3,4)//[4,2,3,4,5][1,2,3,4,5].copywithin (0,-2,-1)  

4, array instance of Find () and FindIndex ()

Used to find the first eligible array member, the parameter is a callback function, the array member executes the callback function one time until the first qualifying member is found,find () returns the eligible member ,FindIndex () Returns the position of the eligible member , and if none is met, find () returns Undefined,findindex () returns-1.

[1,5,10,15].find (function (Value,index,arr) {   return value > 9})//10[1,5,10,15].findindex (function (value, Index, arr) {   return value > 9})//2

In addition, both methods can judge Nan, making up the indexof of the array

[Nan].indexof (NaN)//-1[nan].findindex (y = object.is (nan,y))//0

5, the Fill () of the array instance

Populates the array with the given values. Used for initialization. You can not accept the second and third arguments, or you can accept the next 2 parameters, which specify the position of the fill

[A]. Fill (7)//[7,7,7]

[' A ', ' B ', ' C ']. Fill (7,1,2)//starting at position 1th, ending with position 2nd before end [' A ', 7, ' C ']

6, array instance of entries (), keys () and values ()

are used to iterate through an array, using For....of ... Loop traversal, the only difference is. Keys is a variable traversal of the key name, and values is the traversal of the key value, and entries is the traversal of the key-value pair.

For (Let index of [' A ', ' B '].keys ())} {  Console.log (index)  //0 1  }for (let elem of [' A ', ' B '].values ()) {  Console.log (Elem); ' A ' B '  }for (let [index,elem] of [' A ', ' B ']. Entries ()) {  console.log (index.elem);  0 ' a '      1 ' b '  }

7, array instance of includes ()

The Array.prototype.includes method returns a Boolean value that indicates whether the array contains the given value, or it can accept the second argument, which represents the starting position of the search. Default is 0

[1,2,3].includes (2)//true[1,2,3].includes (3,3)//false

The previous use of the IndexOf method, the disadvantage of using this method is: 1, not enough semantics. 2, the internal use of strict operator = = = Judgment, this will cause a miscarriage of Nan

[Nan].indexof (Nan)  //-1[nan].includes (nan)//true

For browsers that have not yet deployed the method, you can use the Array.some method instead

Const CONTAINS = (() = Array.prototype.includes?                                    (arr,value) = Arr.includes (value)                                 : (Arr,value) = Arr.some (el = el===value)                                   ) ();            

  

Expansion of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.