visual-studio-codecode-snippets

vscode snippets - items in an array


I have simple snippet:

- const ${1:listItems} = ["$3","$4", "$5", "$6"]

ul
  each ${2:item}, i in ${1:listItems} 
    li #{ ${2:item} } 
$0

now its set up to display 4 items ... is there a simple way how to fill items in an array if the number of items is unknown? (one time it can be 5 items another time it can be 12 items)
Do I just put:

- const ${1:listItems} = [$3]

and keep typing or is there another way how to add items?


Solution

  • Choose a separator character, in example I choose |, that is unlikely to be in an item, split argument ${3} on this separator and enclose it in " or substitute it with ,

    Use this as the body of the snippet

        "body": [
          "- const ${1:listItems} = [${3/([^|]+)|([|])/${1:+\"}$1${1:+\"}${2:+, }/g}]",
          "",
          "  ul",
          "    each ${2:item}, i in ${1}",
          "    li #{ ${2} } ",
          "$0"]