"Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable" ("Common Lisp the Language").
Questions tagged [destructuring]
1338 questions
316
votes
7 answers
Types in object destructuring
This
const { foo: IFoo[] } = bar;
and this
const { foo: Array } = bar;
will reasonably cause an error.
And this
const { foo: TFoo } = bar;
will just destructure TFoo property.
How can types be specified for destructured object properties?

Estus Flask
- 206,104
- 70
- 425
- 565
184
votes
17 answers
Is it possible to destructure onto an existing object? (Javascript ES6)
For example if I have two objects:
var foo = {
x: "bar",
y: "baz"
}
and
var oof = {}
and I wanted to transfer the x and y values from foo to oof. Is there a way to do that using the es6 destructuring syntax?
perhaps something like:
oof{x,y}…

tnrich
- 8,006
- 8
- 38
- 59
177
votes
13 answers
One-liner to take some properties from object in ES 6
How one can write a function, which takes only few attributes in most-compact way in ES6?
I've came up with solution using destructuring + simplified object literal, but I don't like that list of fields is repeated in the code.
Is there an even…

kirilloid
- 14,011
- 6
- 38
- 52
163
votes
18 answers
Destructuring to get the last element of an array in es6
In coffeescript this is straightforward:
coffee> a = ['a', 'b', 'program']
[ 'a', 'b', 'program' ]
coffee> [_..., b] = a
[ 'a', 'b', 'program' ]
coffee> b
'program'
Does es6 allow for something similar?
> const [, b] = [1, 2, 3] …

George Simms
- 3,930
- 4
- 21
- 35
152
votes
4 answers
What is the difference between const and const {} in JavaScript
When I study electron, I found 2 ways of getting BrowserWindow object.
const {BrowserWindow} = require('electron')
and
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
What is the difference between const and const…

Kevin00000000
- 1,719
- 2
- 12
- 11
150
votes
4 answers
What does curly brackets in the `var { ... } = ...` statements do?
Not sure if this is a Mozilla-specific JS syntax, but I often found variables being declared this way, for example, in add-on SDK docs:
var { Hotkey } = require("sdk/hotkeys");
and in various chrome Javascript (let statement is being used in place…

timdream
- 5,914
- 5
- 21
- 24
140
votes
2 answers
Destructuring and rename property
const a = {
b: {
c: 'Hi!'
}
};
const { b: { c } } = a;
Is it possible rename b in this case? I want get c and also rename b.

leusrox
- 1,855
- 3
- 10
- 17
139
votes
12 answers
JavaScript variable assignments from tuples
In other languages like Python 2 and Python 3, you can define and assign values to a tuple variable, and retrieve their values like this:
tuple = ("Bob", 24)
name, age = tuple
print(name) #name evaluates to Bob
print(age) #age…

Karl
- 6,035
- 5
- 30
- 39
137
votes
3 answers
Document destructured function parameter in JSDoc
Previously I've always documented my object parameters as follows:
/**
* Description of the function
*
* @param {Object} config - The configuration
* @param {String} config.foo
* @param {Boolean} [config.bar] - Optional value
* @return…

morkro
- 4,336
- 5
- 25
- 35
130
votes
5 answers
Types when destructuring arrays
function f([a,b,c]) {
// this works but a,b and c are any
}
it's possible write something like that?
function f([a: number,b: number,c: number]) {
// being a, b and c typed as number
}

thr0w
- 1,454
- 3
- 10
- 7
126
votes
2 answers
Curly brackets (braces) in Node.js 'require' statement
I am trying to understand the difference between the two 'require' statements below.
Specifically, what is the purpose of the { }s wrapped around ipcMain?
const electron = require('electron')
const {ipcMain} = require('electron')
They both appear…

stefanhorne
- 1,619
- 3
- 16
- 23
95
votes
10 answers
Does C# 7 have array/enumerable destructuring?
In JavaScript ES6, you are able to destructure arrays like this:
const [a,b,...rest] = someArray;
where a is the first element in the array, b is the second, and rest is an array with the remaining elements.
I know in C#7 that you can destructure…

kmc059000
- 2,937
- 1
- 23
- 27
94
votes
3 answers
How to destructure object properties with key names that are invalid variable names?
As object keys are strings they can contain any kind of characters and special characters. I recently stumbled upon an object which I receive from an API call. This object has '-' in it's key names.
const object = {
"key-with-dash":…

larrydahooster
- 4,114
- 4
- 40
- 47
85
votes
3 answers
ES6 Destructuring and Module imports
I was under the impression that this syntax:
import Router from 'react-router';
var {Link} = Router;
has the same final result as this:
import {Link} from 'react-router';
Can someone explain what the difference is?
(I originally thought it was a…

Guy
- 65,082
- 97
- 254
- 325
79
votes
2 answers
How can I ignore certain returned values from array destructuring?
Can I avoid declaring a useless variable when array destructuring when I am only interested in array values beyond index 0?
In the following, I want to avoid declaring a, I am only interested in index 1 and beyond.
// How can I avoid declaring…

KevBot
- 17,900
- 5
- 50
- 68