"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
19
votes
1 answer
Use of colon in object assignment destructuring JavaScript
Working with React.js and React Router:
import React, { Component } from 'react';
const PrivateRoute = ({ component: Component, ...rest }) => (
)
*{ component: Component, ...rest }*
...rest is the use of spread…

Shorn Jacob
- 1,183
- 5
- 22
- 38
18
votes
2 answers
useParams in TypeScript does not allow destructuring
I was following this video ("JWTUser Sessions with ReactJS & GraphQL...") when at this time the guy destructures useParams() method from react-router-dom library.
In my case, that didn't work since I am getting this error:
This is the whole code at…

Maramal
- 3,145
- 9
- 46
- 90
18
votes
2 answers
What is the purpose of `&` before the loop variable?
What is the purpose of & in the code &i in list? If I remove the &, it produces an error in largest = i, since they have mismatched types (where i is &32 and i is i32). But how does &i convert i into i32?
fn largest(list: &[i32]) -> i32 {
…

Muhammad Naufil
- 2,420
- 2
- 17
- 48
18
votes
3 answers
What is destructuring assignment and its uses?
I have been reading about Destructuring assignment introduced in ES6.
What is the purpose of this syntax, why was it introduced, and what are some examples of how it might be used in practice?

Code Maniac
- 37,143
- 5
- 39
- 60
18
votes
3 answers
Destructuring nested objects: How to get parent and its children values?
The function below receives an object that has a property current, which is also an object, and it has selectionStart and selectionEnd properties.
Here, nested destructuring works as expected with Start and End variables, But I also need the value…

soufiane yakoubi
- 861
- 11
- 31
18
votes
3 answers
Destructuring with nested objects and default values
I'm using destructuring to declare some variables like this:
const { a, b, c } = require('./something'),
{ e = 'default', f = 'default'} = c;
Is there a way to make this into single line?
I've tried something like :
const { a, b, c = { e =…

Jose Hermosilla Rodrigo
- 3,513
- 6
- 22
- 38
17
votes
2 answers
How to destructure nested objects in for-loop
var users = [
{ user: "Name1",geo:{lat:'12',long:'13'} },
{ user: "Name2",geo:{lat:'12',long:'13'}, age: 2 },
{ user: "Name2",geo:{lat:'12',long:'13'} },
{ user: "Name3",geo:{lat:'12',long:'13'}, age: 4 }
];
Above is the array of…

Amjad Mehmood
- 245
- 1
- 5
- 14
17
votes
5 answers
How to bind methods when destructuring an object in JavaScript?
How to bind methods when destructuring an object in JavaScript?
const person = {
getName: function() {
console.log(this);
}
};
var a = person.getName;
var b = person.getName.bind(person);
var {getName: c} = person;
person.getName(); //=>…

Paweł
- 4,238
- 4
- 21
- 40
17
votes
9 answers
Handle variable number of out parameters with less code duplication in C#
I'm trying to write a function that populates strings with the contents of an array, or sets them to null. The number of strings is can vary and I don't want to add requirements like them all being part of the same array or class.
In C# you cannot…
user1522973
17
votes
1 answer
ng-annotate error with Babel and destructuring
Strange error when transpiling to ES6 with Babel, ng-annotate doesn't like destructuring. I copied my source into the online babel compiler and it works fine. Commenting out ng-annotate in my gulp pipe chain gets rid of the error. Removing the /*…

Phix
- 9,364
- 4
- 35
- 62
17
votes
2 answers
How do I destructure a vector without taking a slice?
I can destructure a vector of tuples by taking a slice of a vector and references to the items within the tuple:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name,…

Peter Horne
- 6,472
- 7
- 39
- 50
16
votes
2 answers
Destructuring Nested objects in javascript | Destructure second level parent and child Objects
I need to destructure and get values of title, child, childTitle from this object
const obj1 = {
title : 'foo',
child : {
title2 : 'bar'
}
}
let {title, child} = obj1;
console.log(title) // 'foo'
console.log(child) // { title :…

Varun Suresh Kumar
- 868
- 7
- 16
16
votes
3 answers
Destructuring assignment cause error: "unexpected token ="
let's say I call use this syntax in es6:
let a, b;
{a, b} = { a: 100, b: 300 };
the code will run without error;
but let's rewrite is like this:
function fn() {
return { a: 100, b: 200 }
}
let a, b;
{ a, b } = fn();
when I run the code…

碳60
- 173
- 1
- 7
16
votes
2 answers
Destructuring of es6 but passing dynamic variable
Says my state is like this:
{
item:{
a:'a',
b:'b'
}
}
Then I'm able to pull a from item by doing:
const { a } = this.state.item
but can pull dynamically using {} of es6 ?
For example const { variable } = this.state.item, where…

Alex Yong
- 7,425
- 8
- 24
- 41
16
votes
5 answers
Destructure object properties inside array for all elements
In its most basic form, having an array of objects:
let arr = [
{val:"a"},
{val:"b"}
];
How can destructuring be used, to obtain only the values ['a', 'b'].
getting the first value is easy:
let [{val:res}] = arr; //res contains 'a'
Obtaining…

Me.Name
- 12,259
- 3
- 31
- 48