Deconstruction Assignment of arrays

Source: Internet
Author: User

ES6 allows you to extract values from arrays and objects in a certain pattern, assigning values to variables, known as deconstructed
Let [A, B, c] = [1, 2, 3];

The above code indicates that the value can be extracted from the array, and the variable is assigned a value according to the corresponding position.

let [foo, [[Bar], Baz]] = [1, [[2], 3]];foo//1Bar//2Baz//3Let [,, third]= ["foo", "Bar", "Baz"];third//"Baz"Let [x,, Y]= [1, 2, 3];x//1Y//3Let [head, ... tail]= [1, 2, 3, 4];head//1Tail//[2, 3, 4]Let [x, Y, ... z]= [' A '];x//"a"Y//undefinedZ// []

If the deconstruction is unsuccessful, the value of the variable is equal to undefined .

let [foo] == [1];

Both of these cases are deconstruction unsuccessful and foo the values are equal undefined .

The other is not completely deconstructed, that is, the pattern to the left of the equals sign, matching only one part of the array to the right of the equals sign. In this case, the deconstruction can still succeed.

let [x, y] = [1, 2, 3//  1//  2= [1, [2, 3], 4//  1//  2//  4

The above two examples are not completely deconstructed, but can be successful.

For a SET structure, you can also use the destructor of an array to assign a value.

New Set ([' A ', ' B ', ' C '//  ' a ' )

In fact, as long as a data structure has a Iterator interface, it can be used in the form of an array of deconstruction assignment

function* fibs () {  = 0;   = 1;    while (true) {    yield A;     = [B, a + b];   =//  5

In the above code, there fibs is a Generator function, which is native with the Iterator interface. The deconstruction assignment gets the value from this interface in turn.

Default value
true] =//  true//  x= ' A ', y= ' B '//  x= ' A ', y= ' B '

Note that the ES6 internally uses the strict equality operator ( === ) to determine whether a position has a value. Therefore, the undefined default value will only take effect if an array member is strictly equal.

let [x = 1] =//  1= 1] = [null//  null< /c19>

In the above code, if an array member is null , the default value does not take effect because it is null not strictly equal undefined .

If the default value is an expression, the expression is evaluated lazily, which is evaluated only when it is used.

function f () {  console.log (' aaa '= f ()] = [1];

In the above code, because x a value can be taken, the function f does not execute at all. The code above is actually equivalent to the following code.

Let x; if ([1][0] = = = undefined  ) {=else  {  = [1][0];}

The default value can refer to other variables that deconstruct the assignment, but the variable must already be declared.

let [x = 1, y = x] = [];     // x=1; y=1let [x = 1, y = x] = [2];    // x=2; y=2 // x=1; y=2let [x = y, y = 1] = [];     // referenceerror:y is not defined

The last expression above is an error because it is x y not declared when the default value is used y .

Reference: http://es6.ruanyifeng.com/#docs/destructuring

Deconstruction Assignment 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.