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

Is this a bug in Method#to_proc? (Ruby 1.8.7)

Given the following method that takes one argument: def foo(arg); p arg; end I can call it with an empty array: foo([]) # prints [] I can also save it as a Method object and call that with an empty array, with the same…
Sam Stokes
  • 14,617
  • 7
  • 36
  • 33
13
votes
3 answers

Destructuring assignment in function call while preserving the object

Is there a way to do something like the following? f = (o:{a:x}) { console.log(o); console.log(x); } f({a:0}); //Should Print: //{a:0} //0 To get the same result as the this. f = function(o) { var {a:x} = o; console.log(o); …
Diasiare
  • 745
  • 1
  • 6
  • 18
13
votes
3 answers

How to handle nested default parameters with object destructuring?

I am trying to figure out if it is possible to handle multiple levels of default parameters with destructuring. Since it is not easy to explain with words, here is a step-by-step example... 1 - Flat object destructuring with default…
13
votes
1 answer

Unpack array argument directly to parameters?

I know I can do this: function (value: [boolean, string]) { const [boolValue, stringValue] = value; // make use of boolValue and stringValue } But am I able to do something like this? // doesn't work function ([boolValue: boolean,…
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
12
votes
1 answer

TS will not infer possible undefined when destructuring empty array

When looking at the inferred type of destructured element, it will assume the array is never empty. const x: number[] = []; const [first] = x; // first inferred as number console.log(first); // undefined if (first !== undefined) { //…
EcksDy
  • 1,289
  • 9
  • 25
12
votes
2 answers

Can the props in a destructuring assignment be transformed in place?

This works… const { prop1:val1, prop2:val2 ) = req.query val1 = val1.toLowerCase() Though, I'm more inclined to do something like const { prop1.toLowerCase():val1, prop2:val2 } = req.query or const { prop1:val1.toLowerCase(), prop2:val2 } =…
12
votes
1 answer

Nested object destructuring and renaming

Is it possible to rename a variable when destructuring a nested object in JavaScript? Consider the following code: const obj = {a: 2, b: {c: 3}}; const {a: A, b:{c}} = obj; How can I rename c in above code like I renamed a to A? const {a: A, b:{c}:…
darKnight
  • 5,651
  • 13
  • 47
  • 87
12
votes
3 answers

React-native animated.event custom onScroll listener

In official react-native documentation there is a section about Animated.event method. As example they use following code: onScroll={Animated.event( // scrollX = e.nativeEvent.contentOffset.x [{ nativeEvent: { contentOffset: { …
Michal
  • 4,952
  • 8
  • 30
  • 63
12
votes
3 answers

Destructuring nested objects as function parameters

In ES6 we can do: let myFunc = ({name}) => { console.log(name) } myFunc({name:'fred'}) // => logs 'fred' But how do I do it for nested properties like this: myFunc({event:{target:{name:'fred'}}}) // => I want it to log 'fred' What should myFunc…
danday74
  • 52,471
  • 49
  • 232
  • 283
12
votes
2 answers

Method parameter: destructuring + keeping original parameter (ReactJS component)

is there any way to achieve method parameter destructuring, but also be able to get method parameter. In the context of a React application with stateless components, I'd like to be able to replace const MyComponent = (props) => { const {prop1,…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
12
votes
2 answers

How to use destructuring assignment to define enumerations in ES6?

You can use destructuring assignment to define enumerations in ES6 as follows: var [red, green, blue] = [0, 1, 2]; Instead, I'd like the right hand side of the destructuring assignment to be dynamic. For example: var MAX_ENUM_SIZE = 32; var ENUM =…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
12
votes
1 answer

How to destructure option argument with all default values in ES6?

I use ES6 features with babel compiler. I have a function which takes option object as an argument: function myFunction({ option1 = true, option2 = 'whatever' }) { console.log(option1, option2); // do something... } When I call it,…
madox2
  • 49,493
  • 17
  • 99
  • 99
12
votes
1 answer

Where can I get info on the object parameter syntax for JavaScript functions?

If I want to call a function like this: moo({ a: 4 }); Normally I'd have to phrase my function definition like this: function moo(myArgObj) { print(myArgObj.a); } But this awesome syntax is totally valid in spidermonkey for defining…
Verdagon
  • 2,456
  • 3
  • 22
  • 36
11
votes
4 answers

Is it possible to destructure an object into existing variables?

I am trying to extract variables using object destructuring but those variables already exist, something like this const x=1, y=2 // Those should be 1 and 2 const {x,y} = complexPoint const point = {x,y} Is there any way to do this without renaming…
11
votes
1 answer

In ES6 is destructuring Class Instance properties permitted?

Suppose I have the following code: class Foo { constructor() { this.a = 1; this.b = 'something'; } someMethod() { // Is this legal? let { a, b } = this; } } Is the destructuring assignment in…
baseten
  • 1,362
  • 1
  • 13
  • 34