"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
9
votes
2 answers
ES6: change a value while destructuring
Lets say I have an object
const obj = { width: 100, height: 200 }
I wish to pass that object to a method
myFunc( obj );
Inside that method I wish to pull out the height, but at the same time subtract a value. I only wish to do this once and…

delp
- 771
- 11
- 20
9
votes
5 answers
How i can destructuring {this.props.children}?
I want to add a background to my mobile app but when i use "this.props.children"
eslint say me "Must use destructuring props assignment". Why i can destructuring this props ?
This my code ,
export default class WallPaper extends Component {
…

Michel Melhem
- 551
- 1
- 8
- 22
9
votes
3 answers
Destructuring state/props in React
I'm learning React and I have Eslint installed in my project. It started giving me errors like
Use callback in setState when referencing the previous state. (react/no-access-state-in-setstate)
Must use destructuring state assignment…

dragi
- 1,462
- 5
- 16
- 28
9
votes
1 answer
How do you ignore an item when destructuring an array?
I'm self-answering this because I didn't come across a question or answer that discussed ignoring a destructured array element while searching.
Is there a way to ignore an element of a array when destructuring? The closest thing that I can think of…

zero298
- 25,467
- 10
- 75
- 100
9
votes
4 answers
Component destructuring with fewer than expected components
Let's say I want to do the following:
val (k, v) = pair.split("=".toRegex(), 2)
This code is fine if I always get 2 components from the split - however, if the delimiter is not present in the string, this code throws an exception, because the…

Melllvar
- 2,056
- 4
- 24
- 47
9
votes
5 answers
How does this particular scenario of default params and destructuring work?
I was trying some things today and came across a behaviour I would like to understand.
var b = ({a = 1, b = 1, c = 1}) => a + b + c;
b(); // throws error.
But if it is defined like this
var b = ({a = 1, b = 1, c = 1} = 0) => a + b + c;
b() //…

Shyam Babu
- 1,069
- 7
- 14
9
votes
3 answers
What does the es6 { [a]: b } destructuring mean?
There is some destructuring going on here:
const { [a]: b } = this.props
But, what does [a]: b do: what does the brackets with colon do?
In my case, a is supplied as one of the props with a string value.

Rui Nunes
- 798
- 1
- 8
- 15
9
votes
2 answers
Using destructuring to define optional parameters in ES6
I have a function that performs an AJAX call, as such:
let retrieveData = (section, sectionItem, callback) => {
...
}
Where the second parameter is optional, i.e. in some cases that parameter is required, in others it isn't:
let data =…

Ryan
- 767
- 3
- 9
- 31
9
votes
1 answer
F#: Destructuring bind with a discriminated union
open System
let x = (1, 2)
let (p, q) = x
printfn "A %A" x
printfn "B %A %A" p q
let y = Some(1, 2)
try
let None = y
()
with
| ex -> printfn "C %A" ex
let Some(r, s) = y
printfn "D %A" y
// printfn "E %A %A" r…

nodakai
- 7,773
- 3
- 30
- 60
8
votes
2 answers
Destructuring an Option> inside a match statement in Rust
I have a big object that I need boxed in another object but I don't necessarily need it all the time. So I want to use an if statement to get the optional boxed TempStructure but i'm not exactly sure how I can destructure and dereference at the same…

Zyansheep
- 168
- 2
- 11
8
votes
3 answers
Safe destructuring using nullish coalescing or optional chaining
Currently I am using below code for destructuring:
const myObj1 = {name: 'Abc'}
const {name} = myObj1
console.log(name)
const myObj2 = null
const {name2} = myObj2 // this will give error
Now, since we have optional chaining, I can do…

Adarsh Madrecha
- 6,364
- 11
- 69
- 117
8
votes
1 answer
How to document array destructured parameter in JSDoc
Given the following code, how do I properly document that using the latest JSDoc?
function docMe([foo, bar = null, baz = 1]) {
/* */
}
I have tried this:
/**
* @param {Array} options Array containing the options.
* @param {HTMLElement}…

connexo
- 53,704
- 14
- 91
- 128
8
votes
1 answer
Cleaner way to destructure nested objects with defaults?
I have a nested object which may have stuff missing:
const unreliableObject = {
id: 10,
nestedObject: { // may be missing
id: 11 // may also be missing
}
}
Now say I want to get the inner id. I can do the following
const {…

AncientSwordRage
- 7,086
- 19
- 90
- 173
8
votes
2 answers
Destructure a nested object, but keep a reference to the nested object
I have a simple use-case, but I think it's not possible with ES6 syntax. I'd like to use object destructuring to retrieve certain known properties from a nested object, but I'd also like a reference to that nested object so that I can pass it along…

JDB
- 25,172
- 5
- 72
- 123
8
votes
3 answers
Destructuring and re-structuring in function arguments?
I'm trying to use named function arguments and default values using destructuring.
function doSomething({arg1 = "foo", arg2 = "bar"} = {}) {
console.log(arg1, arg2);
}
but I would also like to have access to the entire object, in case the user…

user3056556
- 81
- 1