ES6 Learning notes 4--Array

Source: Internet
Author: User
Tags array length

Expansion of arrays Array.from ()

Array.fromThe Array-like method is used to convert two types of objects to a true array: An array-like object (A. iterable) and an object that can be traversed (including ES6 new data structure set and map).

 let arraylike = ' 0 ': ' A ' ,  ' 2 ': ' C '  3};  //  ES5 the notation  var  arr1 = [].slice.call (arraylike); //  [' A ', ' B ', ' C ']  //  ES6 's notation  let arr2 = Array.from (arraylike); //  [' A ', ' B ', ' C ']  

Array.fromMethods are also supported for arrays-like objects. The so-called array-like object, which has only one essential feature, must have length attributes. Therefore, any length object that has attributes can be converted to Array.from an array by means of the method, and the extension operator cannot be converted at this time.

Array.from ({length:3 }); // [Undefined, undefined, undefinded]

In the above code, an Array.from array with three members is returned, and the values for each position are undefined . The extension operator cannot convert this object.

Array.fromYou can also accept the second parameter, which acts like an array map , to process each element and put the processed value into the returned array.

Array.from (arraylike, x = x * x); // equivalent to Array.from (arraylike). map (x = = x * x); Array.from ([1, 2, 3], (x) = x * x)//  [1, 4, 9]

Array.of ()

Array.ofmethod is used to convert a set of values into an array.

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

The main purpose of this method is to compensate Array() for the insufficiency of the array constructor function. Because of the different number of arguments, Array() there are differences in behavior.

Array.ofAlways returns an array of parameter values. If there are no arguments, an empty array is returned.

Array.ofCan basically be used to replace Array() or new Array() , and there are no overloads caused by different parameters. Its behavior is very uniform.

Copywithin () of an array instance

The method of an array instance, within the copyWithin current array, copies the members of the specified position to another location (overwriting the original member), and then returns the current array. In other words, using this method modifies the current array.

this. Length)

It accepts three parameters.

    • Target (required): Replace data from this location.
    • Start (optional): Reads data from this location, which defaults to 0. If negative, the countdown is indicated.
    • End (optional): Stops reading data before it is reached, which is equal to the array length by default. If negative, the countdown is indicated.

All three parameters should be numeric and, if not, automatically converted to numeric values.

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

The code above indicates that the members (4 and 5), from the 3rd to the end of the array, are copied to the position starting from the No. 0 bit, and the result overwrites the original 1 and 2.

// for platforms that do not have a Copywithin method deployed Typedarray // you need to use the following notation [].copywithin.call (new Int32array ([1, 2, 3, 4, 5]), 0, 3, 4); // Int32array [4, 2, 3, 4, 5]
Find () and FindIndex () for array instances

A method of an array instance that is find used to find the first qualifying array member. Its argument is a callback function, in which all array members execute the callback function sequentially until they find the first member that returns a value true , and then return the member. Returns if there are no members that meet the criteria undefined .

[1, 4, -5, 10].find ((n) = n < 0)//  -5

The above code finds the first member in the array that is less than 0.

The method of an array instance is findIndex find very similar to the method that returns the position of the first qualifying array member, or returns if all members do not meet the criteria -1 .

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

Both methods can accept the second argument, which binds the object to the callback function this .

In addition, both methods can be found NaN to compensate for the lack of array IndexOf methods.

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

In the above code, the indexOf method does not recognize the members of the array NaN , but the method findIndex can Object.is do so.

The fill () of the array instance

fillMethod fills an array with the given value.

[' A ', ' B ', ' C '].fill (7)//  [7, 7, 7]new Array (3). Fill (7)//  [ 7, 7, 7]

The above code shows that the fill method is very convenient for initializing an empty array. The elements that are already in the array are erased.

entries () , keys () and values () --used to iterate over an array. They all return a Walker object (see Chapter "Iterator"), which can be traversed using for...of Loop, the only difference being keys () is the traversal of the key name, values () The is the traversal of the key value, 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"

ES6 Learning notes 4--Array

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.