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
10
votes
1 answer

*why* does list assignment flatten its left hand side?

I understand that list assignment flattens its left hand side: my ($a, $b, $c); ($a, ($b, $c)) = (0, (1.0, 1.1), 2); say "\$a: $a"; # OUTPUT: «$a: 0» say "\$b: $b"; # OUTPUT: «$b: 1 1.1» <-- $b is *not* 1 say "\$c: $c"; # OUTPUT: «$c: 2» …
10
votes
1 answer

Destructuring/list assignment with the `has` declarator

[I ran into the issues that prompted this question and my previous question at the same time, but decided the two questions deserve to be separate.] The docs describe using destructuring assignment with my and our variables, but don't mention…
codesections
  • 8,900
  • 16
  • 50
10
votes
1 answer

Why does JS desctructuring assignment work with numbers

As the title says, why does this code not throw a SyntaxError? I thought you could only destructure Objects const { a, b } = 0; console.log(a, b); // undefined, undefined
10
votes
4 answers

Destructure/access a property that may or may not exist on an object union type

I get the following errors: type Union = { type: "1"; foo: string } | { type: "2"; bar: number }; function doSomething = (object: Union) => { const { foo } = object // ^ TS2339: Property 'foo' does not exist on type 'Union'. …
10
votes
4 answers

Destructure object into an array

I have this object const foo = { a: 'kitten', b: 'puppy', c: 'lion' }; Destructuring it into variables goes like this const { a, b, c } = foo; Is there a one-liner how to desctructre this into an array, so that the result ist const…
four-eyes
  • 10,740
  • 29
  • 111
  • 220
10
votes
1 answer

How can I do object destructuring with condition?

So I have: // some function that returns two arrays .. getArrays() { return { arr1: [...], arr2: [...] }; } // and then .. let arr1 = []; let arr2 = []; if (someCondition) { { arr1, arr2 } = getArrays(); } // here we expect arrays,…
pesho hristov
  • 1,946
  • 1
  • 25
  • 43
10
votes
5 answers

ES6 destructuring object assignment function parameter default value

Hi I was going through examples of object destructuring use in passing function parameters here Object Destructuring Demo function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = **{}**) { console.log(size, cords, radius); //…
Shailesh Vaishampayan
  • 1,766
  • 5
  • 24
  • 52
10
votes
1 answer

Typescript Object destructuring results in "Property assignment expected."

I am transitioning a project from Babel to Typescript and receive the following compiler error: error TS1136: Property assignment expected. from code that looks like this: var auth = {...this.props.auth}; This code previously worked fine under…
Rick
  • 8,366
  • 8
  • 47
  • 76
10
votes
2 answers

Is is possible to destructure a clojure vector into last two items and the rest?

I know I can destructure a vector "from the front" like this: (fn [[a b & rest]] (+ a b)) Is there any (short) way to access the last two elements instead? (fn [[rest & a b]] (+ a b)) ;;Not legal My current alternative is to (fn [my-vector]…
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
10
votes
2 answers

Any good way to declare unused variables in destructuring-bind?

I can't figure, is there any way to put something like _ in erlang, for "unused value" in destructuring-bind? For example there we have something like that: (destructuring-bind ((_SNIPPET (_TITLE . title) …
sheikh_anton
  • 3,422
  • 2
  • 15
  • 23
9
votes
1 answer

Destructuring assignment in object creation

As with my previous question, this is an area where I can't tell if I've encountered a bug or a hole in my understanding of Raku's semantics. Last time it turned out to be a bug, but doubt lightning will strike twice! In general, I know that I can…
codesections
  • 8,900
  • 16
  • 50
9
votes
2 answers

Array Destructuring Skipping Values

My airbnb styleguide told me I should use Array Destructuring for the assignment below. const splittedArr  = [1, 2, 3, 4, 5] const result = splittedArr[1]; So I wrote it like this using skipping values , to get the second element. const…
Aalexander
  • 4,987
  • 3
  • 11
  • 34
9
votes
1 answer

How to destructure an object without one key

I have an object like obj = { test1: 'sth', test2: 'sth', label: 'sth' }. And I would like to destructure this object {...obj} except label to get { test1: 'sth', test2: 'sth' }. How to destructure the object without this key? Should I create a new…
doobean
  • 1,929
  • 4
  • 19
  • 27
9
votes
1 answer

Does Groovy have object destructuring like Javascript?

Does Groovy have object destructuring with multiple assignments like Javascript: let options = { title: "Menu", width: 100, height: 200 }; let {title, width, height} = options; alert(title); // Menu alert(width); // 100 alert(height); //…
dilvan
  • 2,109
  • 2
  • 20
  • 32
9
votes
2 answers

Possible to destructure in JavaScript via bracket notation?

This is not necessarily an issue, more a curiosity that came up via an ESLint error which led me to wonder if there was a better way that just disabling ESLint for this line. Consider the code snippet below. ESLint will give an error if the…
Ben
  • 5,079
  • 2
  • 20
  • 26