Questions tagged [destructuring]

"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").

1338 questions
39
votes
2 answers

Is there a way to destructure a struct partially?

I have a struct: struct ThreeDPoint { x: f32, y: f32, z: f32 } and I want to extract two of the three properties after instantiating it: let point: ThreeDPoint = ThreeDPoint { x: 0.3, y: 0.4, z: 0.5 }; let ThreeDPoint { x: my_x, y: my_y…
Tom
  • 4,776
  • 2
  • 38
  • 50
38
votes
1 answer

Typed function parameters using destructuring and rest in TypeScript

I have a function: export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( ... ); Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to…
user7605119
36
votes
2 answers

Java object destructuring

In javascript there is object destructuring so we can break down objects and just use the end key if the intermidiate objects are resused multiple times. e.g) const person = { firstName: "Bob", lastName: "Marley", city: "Space" } So instead…
lion_bash
  • 1,309
  • 3
  • 15
  • 27
36
votes
8 answers

How do I parse a string to number while destructuring?

I am trying to experiment around destructuring assignment. Now I have a case which I trying to cop up with destructuring itself. For example, I have an input like this: let input = {latitude: "17.0009", longitude: "82.2108"} Where latitude and…
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
36
votes
9 answers

Destructure array to object property keys

I have an array of values like: const arr = [1,2,3]; Is there any way I can use destructuring to create the following output? If not, what is the easiest way I can do this in ES6 (or later)? const obj = { one: 1, two: 2, three: 3 }; I…
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
36
votes
2 answers

Is it possible to destructure instance/member variables in a JavaScript constructor?

Is it possible to use destructuring assignment in a JavaScript class' constructor to assign the instance variables similar to how you can do it to normal variables? The following example works: var options = {one: 1, two: 2}; var {one, two} =…
Aaron
  • 13,349
  • 11
  • 66
  • 105
36
votes
1 answer

ES6/ES2015 object destructuring and changing target variable

How can I rename the target during object destructing? const b = 6; const test = { a: 1, b: 2 }; const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015 // a === 1 // b === 6 // c === 2
Jack Allan
  • 14,554
  • 11
  • 45
  • 57
35
votes
2 answers

Object destructuring with property names that are not valid variable names

Does anyone know if you can use object destructuring with spaces in the property name? Maybe this cannot be done and I realize the JavaScript notation is incorrect but I cannot change the server json response. var obj1 = {name: 'Mr Smith', age:…
keano
  • 691
  • 7
  • 9
35
votes
1 answer

ES6 Destructuring in Class constructor

This may sound ridiculous but bear with me. I wonder if there is support on the language level to destructure object into class properties in constructor, e.g. class Human { // normally constructor({ firstname, lastname }) { …
Lim H.
  • 9,870
  • 9
  • 48
  • 74
34
votes
3 answers

Destructuring Variables Performance

Is there a performance difference, if any, between writing const color = props.color; vs const { color } = props; Also, do we gain or lose any performance if we destructure in the parameters signature? See example3 I assume example3 in this…
Christian Bangert
  • 673
  • 1
  • 6
  • 16
32
votes
4 answers

Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?

For a project, a developer sent us a JS file with code similar to this: var myList = [ 1, 2, 3 ]; var a, b, c; [ a, b, c ] = myList; It works in Opera 10.30, and Firefox 3.6.x, but it’s not okay for Opera 10.60, and Chrome. It’s just curiosity: do…
napolux
  • 15,574
  • 9
  • 51
  • 70
31
votes
1 answer

ES6 — How to destructure from an object with a string key?

I have an object { hello_en: 'hello world', 'hello_zh-CN': '世界您好', something: 'nice day', something_else: 'isn\'t it' } being passed into a function function(data) { const { hello_en, hello_zh-CN, ...rest } = data // do some stuff with…
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
29
votes
3 answers

Why is this valid syntax?

While refactoring some code I accidentally discovered that this is valid syntax (or at least, doesn't cause a parser error in Firefox): const {} = somefunc(); somefunc returns an object and the curly's are supposed to contain variable names for…
DJL
  • 2,060
  • 3
  • 20
  • 39
29
votes
5 answers

ES6 destructuring function parameter - naming root object

Is there a way to retain the name of a destructured function argument? I.e., the name of the root object? In ES5, I might do this (using inheritance as a metaphor to make the point): // ES5: var setupParentClass5 = function(options) { …
jbx
  • 443
  • 4
  • 7
28
votes
1 answer

How can I use my specs for their intended purposes if they are in a separate namespace?

One of the examples in the clojure.spec Guide is a simple option-parsing spec: (require '[clojure.spec :as s]) (s/def ::config (s/* (s/cat :prop string? :val (s/alt :s string? :b boolean?)))) (s/conform ::config ["-server" "foo"…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
1 2
3
89 90