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
0
votes
0 answers

ES6 while destructuring parameters can I access the containing object?

Use case: I would like to use destructuring to access individual field of the object but I would also like to have access to the object as a whole. I know I can do this: function auth(cred) { const {user, pwd} = cred; // rest of the…
k1m190r
  • 1,213
  • 15
  • 26
0
votes
0 answers

Destructuring with Spread in Node 6.5 not working

According to this compatibility table, there should be no problem with destructuring or the spread operator in Node 6.5 However, this code here: const things = { a: 1, b: 2, c: true, d: false }; const { a, b, ...rest } =…
Jona
  • 1,023
  • 2
  • 15
  • 39
0
votes
1 answer

object destructuring on class members

Using ES6, I have a class in which I'm defining some variables and a function that will take an object and assing my variables to the values of it. This is repetitive, so is there any way I can use destructuring assingment to achieve this? class…
Alexandru Pufan
  • 1,842
  • 3
  • 26
  • 44
0
votes
1 answer

Is it possible to apply destructuring to storing a class method in a variable?

I have the following render method that calls another method in its class: render() { const isValidField = this.isValidField(); } Is it possible - and if so is it a good idea - to use destructuring in this case to avoid repeating the method name…
alanbuchanan
  • 3,993
  • 7
  • 41
  • 64
0
votes
2 answers

How can I use Object Structuring in ES6

How can I do the following scenario in ES6 const x = {a: 10, aa: 100, b: 20, bb: 200} const AA = { a: x.a, aa: x.aa } const BB = { b: x.b, bb: x.bb } I am expecting something like const x = {a: 10, aa: 100, b: 20, bb: 200} const AA = ({ a,…
SRAVAN
  • 769
  • 1
  • 7
  • 11
0
votes
1 answer

Destructure and assign to new variable at the same time not working

I am trying to destructure object and assign it to new variable at the same time: let {x} = a = {x: 'cool'}; console.log(a, x); which outputs: //Object { x: "cool" } cool - in Firefox //ReferenceError: a is not defined - in Chrome, babel-node Why…
madox2
  • 49,493
  • 17
  • 99
  • 99
0
votes
4 answers

Pulling the same value out of a series of keys

I'm trying to quickly pull out ‘value’ property from some objects using destructuring.. is there a simple way to get it from this? I think it might be possible with some complicated destructuring thing i haven’t quite grocked. I know I could use…
Damon
  • 10,493
  • 16
  • 86
  • 144
0
votes
1 answer

How to destructure an id out of an array in Rails

I'm getting an array back from redis (trhough a controller for my projects) which I need to destructure in my product view. Array (showing 2 results, but it can many more): ["project-5", "project-4"] The numbers (5, 4) are my project id's…
0
votes
2 answers

Is it possible to destructure an object and generate a new object in a single statement?

const {name, slug, description, parent} = cat; const saneCat = {name, slug, description, parent}; In the first expression, we define four constants by destructuring a messy object. In the second expression, we combine them into a new object. I…
Dan Ross
  • 3,596
  • 4
  • 31
  • 60
0
votes
2 answers

Destructuring statement in chrome/chromium 44 not being recognised

As far as I know, this is valid EcmaScript6: let obj = {foo: "foo", bar: "bar"}; let {foo, bar} = obj; // <- Syntax error here Firefox runs this code just fine, but both Google Chrome and Chromium give me this error: Uncaught SyntaxError:…
Setzer22
  • 1,589
  • 2
  • 13
  • 29
0
votes
2 answers

Elegant way to destructure array, first n elements and rest

I can solve this problem in a few different ways, but I am not sure if there is a more elegant way to do so. Take an array let foo = [1,2,3,4,5] Is there a method using array destructuring that would work like thist: split(array, n) => ... let…
Matt
  • 2,795
  • 2
  • 29
  • 47
0
votes
0 answers

How to perform Array/Object destructuring manually & efficiently?

Array destructuring is super useful: var [a, b, c] = [1, 2, 8]; It looks like it was implemented into Javascript 1.7, but then removed with bug 1083498. Now according to this table it's not supported by any browser except Firefox! :( I wish I could…
brentonstrine
  • 21,694
  • 25
  • 74
  • 120
0
votes
1 answer

Destructuring assignment in Parse.com CloudCode

I'm trying to use destructuring assignment in Cloud Code on Parse.com, but when I run the code, the compiler gives me this error: Result: ReferenceError: Invalid left-hand side in assignment My code is this: [a, b] = foo(); And for example foo is…
0
votes
0 answers

Why can't I assign to multiple variables using mapply/assign?

(As an exercise) I was trying to emulate some languages such as perl, etc by assigning to multiple variables in R. my ($a, $b, $c) = ( 1, 2, 3 ); now $a is 1, $b is 2, $c is 3. I expected this to work: mapply(assign, c('A', 'B', 'C'), 1:3) But it…
0
votes
1 answer

How do I rename "_arg" in a destructured function argument?

I have a reduce function like this: ops = rqOps.reduce (p, { commit: id: cid, type: type }, idx, arr) -> # Do stuff here p , {} which works fine, but now the name of the second argument compiles to _arg. How can I give it a different name?…
zakdances
  • 22,285
  • 32
  • 102
  • 173