28

Take the following line of code

const [component] = router.getMatchedComponents({ ...to })

Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Neil
  • 766
  • 1
  • 8
  • 23

1 Answers1

24

It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.

So here in your code:

const [component] = router.getMatchedComponents({ ...to })

You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • 2
    Thanks! so this would mean in the example I posted its essentially doing an array shift on the array returned by router.getMatchedComponents({ ...to }) and setting component to this right? – Neil Nov 14 '17 at 14:04
  • @Neil Yes, kind of. but it's assigning it to a new variable. – cнŝdk Nov 14 '17 at 14:05