2

I'm using the following syntax and getting an error Unexpected token when babel transpiles my script. But when I run the same script in their 'try it out' online compiler, it runs fine. What is wrong with my Object Destructuring and or Rest parameters?

function onDestructure({one,two,three,...ten}) {
    console.log(ten)
}
colecmc
  • 3,133
  • 2
  • 20
  • 33

1 Answers1

5

Rest syntax in object destructuring is not in ES6. You've probably enabled the "experimental" features in the online compiler.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Can you suggest a way to get the unnamed parameters? – colecmc Jul 06 '15 at 16:23
  • @colecmc: What do you mean by "unnamend parameters"? How are you calling that function? – Bergi Jul 06 '15 at 16:24
  • The object I receive has a bunch of keys, but I only need the first few `{one,two,three}` for the moment. If I need the others, is there an alternative to naming each one? `{one,two,three,four,five,six,seven,eight,nine,ten,eleven}` I guess I can use standard syntax at that point by just saying `data` and grabbing what I need: `data.one, data.two` – colecmc Jul 06 '15 at 16:27
  • 3
    Just use `function(options={}) { const {one, two, three} = options; …}`, then you can access all of them dynamically as properties of `options`. – Bergi Jul 06 '15 at 16:34
  • That's perfect. Thank you again! – colecmc Jul 06 '15 at 16:38