"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
16
votes
3 answers
Nested es6 array destructuring?
Is it possible to nest destructuring?
E.g. I want the first item in an array and in that array I want the first item of that child array.
Given:
let arr = [['foo'], ['bar']];
Is there an easier way to do:
let firstItem = arr[0][0];

chrisjlee
- 21,691
- 27
- 82
- 112
16
votes
4 answers
Destructuring assignment in JavaScript
As can be seen in the Mozilla changlog for JavaScript 1.7 they have added destructuring assignment. Sadly I'm not very fond of the syntax (why write a and b twice?):
var a, b;
[a, b] = f();
Something like this would have been a lot better:
var…

Anders Rune Jensen
- 3,758
- 2
- 42
- 53
15
votes
2 answers
How to perform assignment destructuring using the walrus operator in Python
I can do an assignment destructuring as:
a, b = s.split(' ', 1)
for a string s which has more than one word.
How can we do the same in, say an if or elif, with the latest assignment expression introduced in Python 3.8 (is it possible to have…

Austin
- 25,759
- 4
- 25
- 48
15
votes
2 answers
Destructure object parameter, but also have reference to the parameter as an object?
With ES6 you can destructure objects in function arguments:
({name, value}) => { console.log(name, value) }
The equivalent ES5 would be:
function(params) { console.log(params.name, params.value) }
But what if I would like a reference both to the…

webketje
- 10,376
- 3
- 25
- 54
15
votes
1 answer
Renaming remaining properties variable when object destructuring in TypeScript
EDIT:
I opened an issue related to this on github:
https://github.com/Microsoft/TypeScript/issues/21265
It seems that { ...other: xother } is not valid JS, neither TS, code and it should not even compile.
Original question:
Let's assume the…

Tobías
- 6,142
- 4
- 36
- 62
15
votes
1 answer
What is Serilog destructuring?
What is the purpose of Serilog's @ syntax?
If I run the following:
var dummy = new { Foo = "Bar", Date = DateTime.Now };
Log.Information("Dummy object: {Dummy}", dummy);
Then I get an output to the console like so:
Time: 16:20 [Level: Information]…

BanksySan
- 27,362
- 33
- 117
- 216
15
votes
2 answers
ES6 Structuring Assignment?
The new destructuring assignment features of ES6 are fairly well known now (live copy on Babel's REPL); in the case of variables that already exist:
let a, b; // Existing variables
let o = {a: "a", b: "b"}; // An object to get values…

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875
14
votes
1 answer
Cannot destructure property of null
I have a component that destructures user from its auth prop:
const Profile = ({
auth: {user}
}) => {...}
The problem is that when I am developing, Nodemon keeps refreshing my page whenever I save any changes. When the component tries to…

Mike K
- 7,621
- 14
- 60
- 120
14
votes
3 answers
Avoid an error when destructuring from undefined
Lets say I have this code:
const {x, y} = point;
Babel will turn this into:
var _point = point,
x = _point.x,
y = _point.y;
Which is fine, but what if point is undefined? Now I get an error:
"Cannot read property 'x' of undefined".
So…

Jack Allan
- 14,554
- 11
- 45
- 57
14
votes
3 answers
how does destructuring array get length property
I came across this destructuring expression in an article.
const words = ['oops', 'gasp', 'shout', 'sun'];
let { length } = words;
console.log(length); // 4
How does length get the value of 4? I know .length is a property of the array, but how…

leonormes
- 979
- 10
- 29
14
votes
2 answers
ES6/Next: object destructuring with rest - grouping
I have:
const props = {
gallery: [],
select: () => null,
one: 1,
two: 2,
}
I can destructure it with:
const {gallery, select, ...other} = props
I will have three variables now:
gallery = []
select = () => null
other = {one: 1,two: 2}
Is…

Kocur4d
- 6,701
- 8
- 35
- 53
14
votes
2 answers
Why is it possible to pass in key value pairs to a function that destructures a map?
I thought I understood destructuring, but I was reading a clojure blog and this confused me. If you have a function written like:
(defn f [& {:keys [foo bar]}]
(println foo " " bar))
Why can you call it like this:
(f :foo 1 :bar 2)
My first…

Daniel Kaplan
- 62,768
- 50
- 234
- 356
13
votes
7 answers
Get item in sub array and put back to main array
I have a problem when get item in sub array and put back to main array by javascript.
I have array like this:
var array_demo = [
['1st', '1595', '8886'],
['2nd', '1112']
]
I want to get result like this:
['1595','8886','1112']
But when I…

Nghia Phan
- 191
- 2
- 10
13
votes
1 answer
Typescript: How to type rest object destructure
If I have these two objects
interface Input {
one?: string;
two?: string;
c?: string
}
interface Data {
one?: string;
a?: string;
b?: string;
c?: string
}
currently I have to do something like
const { one, two,…

joshhunt
- 5,197
- 4
- 37
- 60
13
votes
2 answers
TypeError: Cannot destructure property `db` of 'undefined' or 'null'
I am getting a TypeError in my variable assignment for mongodb connection. Is there a workaround for this?
//server.js
var mongoose = require('mongoose');
var config = require('./config');
var { db: {user,pass,host,port,name } } = config;
var…

temesgen
- 161
- 1
- 2
- 8