0

I'm learning how to use Nuxt to build generate a static blog, and I came across the piece of code bellow to create the page containing a list of posts:

<script>
  export default {
    async asyncData() {
      const resolve = require.context("~/content/", true, /\.md$/)
      const imports = resolve.keys().map((key) => {
        const [, name] = key.match(/\/(.+)\.md$/);
        return resolve(key);
      });
      return {
        posts: imports
      }
    },
  }
</script>

I understand what it does: getting a list of all the markdown files and map their keys to the file's name, but I don't understand what const [, name] means, actually what the coma inside the array means. can somebody explain it to me, please?

Thanks.

Noah

Noah
  • 13
  • 1
  • 3
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Ignoring_some_returned_values – skirtle Dec 14 '19 at 14:29

2 Answers2

1

It's called array destructuring.

In your case const [, name] = key.match(/\/(.+)\.md$/); is the same as const name = key.match(/\/(.+)\.md$/)[1]

Federkun
  • 36,084
  • 8
  • 78
  • 90
0

It means take the second value from the array returned by key.match and assign it to the variable name

const [a, b] = [123, 456];
console.log('a:', a, 'b:', b);   // a = 123, b = 456

const [, d] = [111, 222];
console.log('d:', d);            // d = 222
gman
  • 100,619
  • 31
  • 269
  • 393