javascriptinquirerinquirerjs

Removing commas from Array created with checkbox from Node Inquirer


I have the following inquirer prompt, which as far as I understands returns an array of strings:

 {name: "food",
        message:"choose your favorite ➝ ",
        type: "checkbox",
        choices: ["option1", "option2", "option3", "option4", "option5", "option6"],
        when: function(answers) {
          return answers.client;
        },
        validate: function(choices) {
            return choices.length <= 3 ? true : "Please select at least 3 choices";
        }

And then I want to print the answer as follows:

let foods = answers.food ? "You chose the following: \n " + answers.food.map(option => "• " + option + "\n") : "";

Hoping to get something like:

You chose the following: • option1 • option2 • option3

And Instead I'm getting something like this:

• option1 ,• option2 ,• option3

Anyone has an Idea of how can I remove those annoying commas?


Solution

  • You are concatenating an array to a string, JS deals with this by calling Array#join with a parameter of ,. So with that in mind you can do let foods = answers.food ? "You chose the following: \n " + answers.food.map(option => "• " + option + "\n").join('') : "";