"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
7
votes
1 answer
Destructured parameters causing eslint no-shadow error
My understanding is destructuring like this will pick properties from the incoming argument object:
const last = "Smith" // unrelated const
const full = function({ first, last }) {
return [first, last].filter(v => v).join(" ")
}
full({last:…

ryanve
- 50,076
- 30
- 102
- 137
7
votes
3 answers
Error while trying to destructure an array of possible length 2
I am trying to destructure an array of length 2, but I get a typescript error:
[ts] Tuple type '[string]' with length '1' cannot be assigned
to tuple with length '2'.
let output = {status: false};
if(execute.permission) {
let…

Suhail Gupta
- 22,386
- 64
- 200
- 328
7
votes
1 answer
How to add TypeScript types to destructured parameters using spread syntax?
Ignore the fact that this is bad add function. It's a question about using array destructuring with spread syntax in TypeScript.
This is what I'm trying
const add = ([x,...xs]) => {
if (x === undefined)
return 0
else
return x +…

Mulan
- 129,518
- 31
- 228
- 259
7
votes
2 answers
Why can't you destructure to a tuple with only one value?
In C# 7, it's apparently not possible to destructure to a tuple with only one item.
ValueTuple exists, so it's not because of that.
And backwards compatibility means a Deconstruct method with one argument must also be legal:
public void…

Drew Noakes
- 300,895
- 165
- 679
- 742
7
votes
2 answers
Understanding the Context of Curly Brackets '{}'
I have been reviewing other people's code and while ES2015 on the whole is taking some getting use to, however, I keep on getting stuck with Destructuring.
Previously, In Javascript, the Curly Brackets {} were either used for blocks or objects.…

Kayote
- 14,579
- 25
- 85
- 144
7
votes
2 answers
How can I mix optional keyword arguments with the & rest stuff?
I have a macro that takes a body:
(defmacro blah [& body] (dostuffwithbody))
But I'd like to add an optional keyword argument to it as well, so when called it could look like either of these:
(blah :specialthingy 0 body morebody lotsofbody)
(blah…

Rayne
- 31,473
- 17
- 86
- 101
7
votes
4 answers
Destructuring Maps in clojure -- unused keys
I have defined a function which takes a map. I thought to use destructuring to access the values. However, I also want to check whether there are any used keys.
So, for example something like...
(defun func1 [{:keys [a b c] :rest rest}]
…

Phil Lord
- 2,917
- 1
- 20
- 31
6
votes
3 answers
Destructuring a JavaScript Array by a Variable Index
To my understanding, JavaScript arrays can be destructured according to a specific index by doing the following.
const { 3: selectedByIndex } = arr;
This would assign the value of the element at index 3 to the variable selectedByIndex.
Is there a…

shprecure
- 61
- 3
6
votes
3 answers
How do you destructure named capture groups?
In JavaScript, using named capture groups is pretty convenient:
const auth = 'Bearer AUTHORIZATION_TOKEN'
const { groups: { token } } = /Bearer (?[^ $]*)/.exec(auth)
console.log(token) // "AUTHORIZATION_TOKEN"
When I try that in typescript,…

Daniel Kaplan
- 62,768
- 50
- 234
- 356
6
votes
1 answer
Why is array destructuring not working in this case?
I am trying to swap members of arr.
But I am getting [0, 0] as output.
I am expecting [1, 0].
const arr = [0, 1];
[arr[1],arr[0]] = arr;
console.log(arr)
But while doing this, it outputs
[1, 0] which I think I understand.
const arr = [0,…

Utshav Dhakal
- 69
- 1
6
votes
2 answers
Remove a property using object destructuring, without eslint no-unused-vars error
Consider the following code where I create a "copy" of x with one of the properties removed, using destructuring:
const x = { a: 1, b: 2, c: 3};
const { a, ...x2} = x;
console.log(x);
console.log(x2);
If I do it like this,…

Dániel Kis-Nagy
- 2,255
- 2
- 18
- 19
6
votes
0 answers
Why do I need parentheses around destructuring assignment?
Suppose I have this function:
const testFunction = () => {
const item_one = 1
const item_two = 2
return { item_one, item_two }
}
When I'm destructuring the return value from this function like this
let item_one, item_two; // the variables are…

Igor Moraru
- 7,089
- 1
- 12
- 24
6
votes
3 answers
How can I select only a few fields when destructuring a JavaScript object?
I can't seem to remember how to write this destructuring pattern, could you please help me?
Let's say I have an object like this:
{
id: 'po_495743057439095',
object: 'payout',
amount: 18560,
arrival_date: 1576195200,
automatic: true,
…

Vincent
- 3,945
- 3
- 13
- 25
6
votes
2 answers
How to shadow existing variables when destructuring in C++?
Is there any way to shadow existing variables when destructuring a std::pair? For example if I have the following functions defined:
#include
#include
std::pair returns_pair(int a, int b)
{
return std::make_pair(a…

ruohola
- 21,987
- 6
- 62
- 97
6
votes
3 answers
How to update a single property in a React state (function component)
I want to update a state with a couple of values from a form with a single change handler in a function component. I cannot figure out how to set a single property in the state in a proper way. I have seen this works in a class component.
import…

thiloilg
- 1,733
- 2
- 18
- 23