Array destructuring

Array destructuring

The simplest form of destructuring is array destructuring assignment:

let input = [1, 2];
let [first, second] = input;
console.log(first); // outputs 1
console.log(second); // outputs 2

This creates two new variables named first and second. This is equivalent to using indexing, but is much more convenient:

first = input[0];
second = input[1];

Destructuring works with already-declared variables as well:

// swap variables
[first, second] = [second, first];

And with parameters to a function:

function f([first, second]: [number, number]) {
  console.log(first);
  console.log(second);
}
f(input);

You can create a variable for the remaining items in a list using the syntax ...name:

let [first, ...rest] = [1, 2, 3, 4];
console.log(first); // outputs 1
console.log(rest); // outputs [ 2, 3, 4 ]

Of course, since this is JavaScript, you can just ignore trailing elements you don’t care about:

let [first] = [1, 2, 3, 4];
console.log(first); // outputs 1

Or other elements:

let [, second, , fourth] = [1, 2, 3, 4];
doc_TypeScript
2016-10-04 19:25:01
Comments
Leave a Comment

Please login to continue.