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
78
votes
4 answers

Destructuring assignment in php for objects / associative arrays

In CoffeeScript, Clojure, ES6 and many other languages we have destructuring of objects/maps/etc somewhat like this: obj = {keyA: 'Hello from A', keyB: 'Hello from B'} {keyA, keyB} = obj I've found the list function in php which lets you…
Cotten
  • 8,787
  • 17
  • 61
  • 98
77
votes
3 answers

Destructuring in Node.JS

This recent video claims that EMCAScript 6 destructuring is already partially implemented in Node.JS. I have tried various examples (using v0.10.12 and the --harmony flag), such as var [a, b] = [1, 2]; and var {a: a, b: b} = {a: 1, b: 2}; to no…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
74
votes
1 answer

How to destructure an object with a key containing a hyphen into a variable?

How do I destructure a property from an object where the key contains a hyphen? Eg: { accept-ranges:"bytes", cache-control:"public, max-age=0", content-length:"1174", content-type:"application/json", date:"Mon, 03 Oct 2016 06:45:03 GMT", …
Sathish
  • 2,056
  • 3
  • 26
  • 40
73
votes
4 answers

How can I emulate destructuring in C++?

In JavaScript ES6, there is a language feature known as destructuring. It exists across many other languages as well. In JavaScript ES6, it looks like this: var animal = { species: 'dog', weight: 23, sound: 'woof' } //Destructuring var…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
71
votes
8 answers

How do I fetch multiple hash values at once?

What is a shorter version of this?: from = hash.fetch(:from) to = hash.fetch(:to) name = hash.fetch(:name) # etc Note the fetch, I want to raise an error if the key doesn't exist. There must be shorter version of it, like: from, to, name =…
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
70
votes
3 answers

React - defaultProps vs ES6 default params when destructuring (performances issues)

I just came across a question about React performances when settings default values in one of my stateless functional components. This component had a defaultProps which defined row: false, but I didn't like it because the defaultProps is at the end…
Vadorequest
  • 16,593
  • 24
  • 118
  • 215
68
votes
4 answers

Split a string straight into variables

I’d like to know if standard JS provides a way of splitting a string straight into a set of variables during their initial declaration. For example in Perl I would use: my ($a, $b, $c) = split '-', $str; In Firefox I can write var [a, b, c] =…
nb5
  • 683
  • 1
  • 5
  • 4
66
votes
16 answers

React JS Error: Invalid attempt to destructure non-iterable instance

I have a sort filter that takes an array to populate the options. Trying to see the option value equal to the text within the array but I get the error within the title: Invalid attempt to destructure non-iterable instance I need to pass the text…
Filth
  • 3,116
  • 14
  • 51
  • 79
59
votes
2 answers

How to mix const and let when using object or array destructuring assignment in ES6?

Example : const foo = {a: "A", b: "B"} const {a, b} = foo What if I want b to be a variable using let ?
PhilipGarnero
  • 2,399
  • 4
  • 17
  • 24
53
votes
4 answers

Destructuring object and ignore one of the results

I have: const section = cloneElement(this.props.children, { className: this.props.styles.section, ...this.props, }); Inside this.props, I have a styles property that I don't want to pass to the cloned element. How can I do?
Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
52
votes
1 answer

Python assignment destructuring

These three expressions seem to be equivalent: a,b,c = line.split() (a,b,c) = line.split() [a,b,c] = line.split() Do they compile to the same code? Which one is more pythonic?
sds
  • 58,617
  • 29
  • 161
  • 278
51
votes
1 answer

Can I pre-declare variables for destructuring assignment of objects?

Background When I tried destructuring assignment with arrays I was able to pre-declare my variables: let a, b, c; let arr = [1, 2, 3, 4, 5]; [a, b, c] = arr; console.log(a) // logs 1 console.log(b) // logs 2 console.log(c) // logs 3 This went…
Lucy Bain
  • 2,496
  • 7
  • 30
  • 45
50
votes
5 answers

How do I destructure all properties into the current scope/closure in ES2015?

I'd like to do something like this: const vegetableColors = {corn: 'yellow', peas: 'green'}; const {*} = vegetableColors; console.log(corn);// yellow console.log(peas);// green I can't seem to find or figure out how to do this but I really…
Resist Design
  • 4,462
  • 3
  • 22
  • 35
40
votes
4 answers

ES6 destructuring within a return statement

Is it possible to destructure an object while returning it at the same time. For example, to change this code: const mapStateToProps = ({ newItem }) =>{ const { id, name, price } = newItem; return { id, name, price }; } To something like…
kfcobrien
  • 481
  • 1
  • 5
  • 11
39
votes
2 answers

destructuring assignment default value

I am learning javascript and I got kind of stuck with ES6 syntax while trying to give a default value to a variable when destructuring. Basically, I am trying to assign a variable giving the value of an object's property to it and if the value is…
1
2
3
89 90