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
6
votes
3 answers

Typescript destructuring with required parameter

Edit Sorry my question was a bit unclear. I want to enforce that the getList Parameter is always required. So I don't have a default value for it. e.g I want the user always to supply a getlist I'm trying to create a constuctor with some optional…
johnny 5
  • 19,893
  • 50
  • 121
  • 195
6
votes
1 answer

what is the correct way to destructure a nested json object with fetch?

I am trying to pull out the license array of numbers for a chart.js The shape of the API report data is: { "report": { "usage": { "chartLabels": [ "'1-Mar', '2-Mar', '3-Mar', '4-Mar', '5-Mar', '6-Mar',…
Gosmith
  • 457
  • 1
  • 3
  • 19
6
votes
3 answers

JavaScript Destructure and assign to new object

In JavaScript/Typescript, What is the short version to destructure and then assign in a new object like so : const payload: MyPayload = { a: 1, b: 2, c: 3, d: 4, e: 5 } // Destruct const { a, c, e } = payload; // New Obj const newPayload = { a,…
Scaraux
  • 3,841
  • 4
  • 40
  • 80
6
votes
1 answer

Typescript: destructuring an object with symbols as keys

Why this code produces an error Type 'symbol' cannot be used to index type '{ [x: string]: string; }'.: let symbol = Symbol() let obj = { [symbol] : 'value'} let { [symbol]: alias } = obj // ^^^^^ the error is…
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
6
votes
1 answer

Why is this JavaScript not interpreted as a code block when semi-colon is used?

In Chrome version ^72 if I run the following JavaScript there are no errors. { prop: p } = { prop: 'prop' } >> { prop: 'prop' } So the line of code is interpreted as an expression statement, unexpectedly. But if I run the same code with a…
6
votes
1 answer

Why destructuring declaration cannot be used in when expression?

Probably this question should go the authors of Kotlin, but I'm sure that on SO there are many Kotlin users with deep knowledge of its architecture. So my question is: why the language doesn't support destructuring in when expressions? For example I…
Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
6
votes
1 answer

What causes "java.lang.IllegalArgumentException: No value supplied for key"?

I have code of the shape (let [{foo :foo} (make-foo)] ...) This code occasionally emits a java.lang.IllegalArgumentException: No value supplied for key: {:foo "foo" :other "other"}. I've seen Clojure : "java.lang.IllegalArgumentException: No value…
user12341234
  • 6,573
  • 6
  • 23
  • 48
6
votes
5 answers

Get max values from each array of objects

So I have data like this: data = [ [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}], [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}], [{a: "d", value: 55}, {a: "dd", value: 9}, {a:…
kay
  • 1,369
  • 4
  • 15
  • 27
6
votes
1 answer

destructure object in javascript es6 when keys are integers

When we have an object in JS like var obj = {a: "apple", p: "pen"}; then we can destructure it as follows var {a, p} = obj; /* a = 'apple', p = 'pen' */ i want to know in case when keys are integers, how can we destructure it ? since we cannot…
Vikramaditya
  • 5,444
  • 6
  • 34
  • 45
6
votes
2 answers

Destructuring forms and Compojure?

I'd thought I'd post this as I got it to work through guesswork without a real understanding of what's going on and I thought it might be helpful if someone explained it. I understand how to get at an element of the :params map in a Compojure…
edoloughlin
  • 5,821
  • 4
  • 32
  • 61
6
votes
3 answers

TypeScript: SyntaxError: "Unexpected token" message in console

Why this code doesn't work? I have the following app.ts var a = 1; var b = 3; console.log(`Before a = ${a}, b = ${b}`); [a, b] = [b, a]; console.log(`After a = ${a}, b = ${b}`); when I try to run it with node app.ts I have the next: [a, b] = [b,…
venber
  • 61
  • 1
  • 1
  • 4
6
votes
1 answer

Destructuring assignment in while loop in ES6 function doesn't propogate out of loop?

I was implementing a simple GCD algorithm in ES6 (through node-esml) and came upon (to me) strange behaviour with updating values of variables inside a while loop. This code works fantastically: function gcdWithTemp(x, y) { let [r, rdash] = [x,…
Steve Cockram
  • 159
  • 2
  • 9
6
votes
1 answer

React, Why the {} when passing props to stateless functional component?

My feeling is that the ({course}) syntax is extracting the property 'course' from the props object. Thereby making calls to 'course' properties inside the component more terse. If I passed props using the (props) syntax, in place of ({course}). I…
malexanders
  • 3,203
  • 5
  • 26
  • 46
6
votes
3 answers

Why can't I skip parameter assignments in a function signature?

With array destructuring, it's possible to discard leading items by inserting commas without a preceding reference: const [ , two ] = [ 1, 2 ] The same isn't true of function signatures — the following code won't parse because the leading comma in…
Barney
  • 16,181
  • 5
  • 62
  • 76
6
votes
5 answers

Swap tuple elements with destructuring assignments

I thought to swap the elements of a tuple in place using destructuring assignment as follows: var a = [1,2]; [a[1], a[0]] = a; However, this yields [1, 1]. Babel compiles this as a[1] = a[0]; a[0] = a[1]; I would have thought this should be…
user663031