ES6-05: Expansion of arrays

Source: Internet
Author: User
Tags javascript array

ES6 the extension of the JavaScript array object is mainly to enhance the creation of the array and the operation of the elements, in particular, an operator, array creation method, array element operation method, array property operation method four aspects;

1. Add an operator--extension operator ...

Extension operator (spread): Like the inverse of the rest parameter, converts an array to a comma-delimited sequence, and the extension operator internally calls the Interator interface of the data structure, so that the object that has the main iterator interface can use the extension operator ; Alternative to the Apply method;

console.log(...[1,2,3]);//1 2 3console.log(1,...[2,3,4],5); // 1 2 3 4 5let a=[1,2,3];let b=[1,2,3];a.push([4,5]); // [1,2,3,[4,5]]b.push(...[4,5]);//[1,2,3,4,5]
2. New Creation Method
    • Array.from(obj): Converts a Class array object (with the Length property), a traversal object (including Set/map) to a true array;
    • Array.of(arugs): Converts a set of values to an array, such as: Array.of(3);//[3] , Array.of(1,2,3);//[1,2,3] ;
    • Array.fill(value,startIndex,endIndex): fills the specified position value in the array with the specified value, and fills all null elements in the array without the second and third arguments;
3. New element Operation method
    • Array.copyWithin(targetIndex,startIndex=0,endIndex): Copies the current array interior to another location (overwriting the original value) and returns the new array, changing the current array;
    • Array.find(fn(value,index,arr){},thisObj): Finds the first eligible member in the array, returns true if found, false otherwise;
    • Array.findIndex(fn(value,index,arr){},thisObj): Finds the first eligible member in the array, returns index if found, otherwise returns-1;
    • Array.includes(value,startIndex): Starts at the specified position in the array and checks to see if the array contains the element value;
4. New Array Property Method:
    • Array.keys(): Gets all the key names in the array, returning a Walker object;
    • Array.values(): Gets all the values in the array, returning a Walker object;
    • Array.entries(): Gets all the key-value pairs in the array, returning a Walker object;

The empty elements in an array of ps:es6 are uniformly processed into undefined, while the ES5 handles the vacancy processing: ①map () skips the vacancy, but retains the value, ②foreach (), filter (), every (), some () will skip the vacancy; ③join (), ToString () treats the vacancy as undefined, while undefined, NULL is processed into an empty string;

ES6-05: 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.