"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
8
votes
5 answers
javascript es6: use case for destructuring rest parameter
I just saw a code snippet in MDN about destructuring rest parameters like so:
function f(...[a, b, c]) {
return a + b + c;
}
f(1) // NaN (b and c are undefined)
f(1, 2, 3) // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not…
user3303864
8
votes
1 answer
ES6 Named Object Parameter Destructuring
I am currently using the object destructuring pattern with default parameters described in that answer to ES6 Object Destructuring Default Parameters.
(function test({a = "foo", b = "bar"} = {}) {
console.log(a + " " + b);
})();
I would like to…

Jared Deckard
- 633
- 6
- 15
8
votes
5 answers
Typescript swap array Items
how to swap two elements using typescript
elements:elements[] =[];
elements.push(item1);
elements.push(item2);
elements.push(item3);
elements.push(item4);
elements[0] is item1
elements[3] is item4
How can i interchange these items in…

Jagadeesh Govindaraj
- 6,977
- 6
- 32
- 52
8
votes
2 answers
How can I spec a hybrid map?
After writing this answer, I was inspired to try to specify Clojure's destructuring language using spec:
(require '[clojure.spec :as s])
(s/def ::binding (s/or :sym ::sym :assoc ::assoc :seq ::seq))
(s/def ::sym (s/and simple-symbol? (complement…

Sam Estep
- 12,974
- 2
- 37
- 75
8
votes
1 answer
object destructuring: how to use intermediate nested property
var { iWantThis: { andThis, andThisToo } } = x;
Is there a way to get access to all three in one destructuring call? I want to avoid two calls like so:
var { iWantThis } = x;
var { andThis, andThisToo } = iWantThis;

Kelly Selden
- 1,238
- 11
- 21
8
votes
2 answers
Is Babel's implementation of ES6 object destructuring correct?
So basic desctucturing is fine, {a, b} = obj transpiles to a = obj.a; b = obj.b.
My question is around a bit of an odd syntax that I accidentally ran across and I'm wondering if someone can point me at spec since I can't find it:
({a, b} =…
user578895
8
votes
1 answer
Constant declaration with block
Recently I was looking into Firefox Add-on Builder SDK sources, and stumbled on such constants declaration:
const { getCodeForKey, toJSON } = require("../../keyboard/utils");
I could find information about CommonJS Modules, but left part of this…

Nash Bridges
- 2,350
- 14
- 19
7
votes
4 answers
Map restructuring
In clojure, I can destructure a map like this:
(let [{:keys [key1 key2]} {:key1 1 :key2 2}]
...)
which is similar to CoffeeScript's method:
{key1, key2} = {key1: 1, key2: 2}
CoffeeScript can also do this:
a = 1
b = 2
obj = {a, b} // just like…

benekastah
- 5,651
- 1
- 35
- 50
7
votes
2 answers
React - TypeScript destructuring of props
I have a function:
export function getSubjectsForStudent(data: any) : any[]
"data argument" I receive from external source and it's not feasible to define strong type.
"return" is derived from "data" so it's of type any as well.
A "Main"…

Albert Lyubarsky
- 438
- 1
- 7
- 15
7
votes
2 answers
How do I prevent code wrapping in Prettier / ESLint
I am struggling to find a setting in my Prettier / ESLint config which allows me to wrap my code like this:
var [
first,
second,
third,
etc,
] = data();
When I hit save, it always turns the code to this automatically:
var [first, second,…

David Haase
- 179
- 1
- 8
7
votes
3 answers
Not nullable value required to call 'component1()' function of destructuring declaration initializer
Is it possible to make the following code to compile in Kotlin?
val variable: String? = "string"
val (a, b) = variable?.run {
1 to 2
}

Diego Marin Santos
- 1,923
- 2
- 15
- 29
7
votes
1 answer
TypeError: Invalid attempt to destructure non-iterable instance React/Jest
I am trying to do a SnapShot verification using Jest for my React App for one of the functional component. Here is the component and test file using Jest
import React, { useState } from 'react';
import useForm from 'react-hook-form';
import {…

siddhuKantipudi
- 303
- 4
- 11
7
votes
2 answers
Ruby destructuring in function parameters
Can you destructure function parameters directly in Ruby :
def get_distance(p1,p2)
x1,y1 = p1
x2,y2 = p2
((y2-y1)**2 + (x2-x1)**2)**0.5
end
I tried the obvious :
def get_distance([x1,y1],[x2,y2])
((y2-y1)**2 + (x2-x1)**2)**0.5
end
But the…

Cliff Stamp
- 531
- 5
- 11
7
votes
2 answers
"Destructuring" a Map.Entry in a Scala closure
val m: java.util.Map[String, Int] = ...
m.foreach { entry =>
val (key, value) = entry
// do stuff with key and value
}
Is there a better way to destructure the Map.Entry? I tried the following, but it does not compile:
m.foreach { (key, value)…

Ralph
- 31,584
- 38
- 145
- 282
7
votes
3 answers
Why isnt this object destructuring working?
The syntax looks to be right off of MDN, so I'm not understanding why this object destructuring isn't working. The variables return undefined, why?
let obj={age: "3", name: "spike"};
let {a,b}=obj;//returns a and b as undefined, why?

what
- 83
- 1
- 3