0

我有以下询问者提示,据我所知,它返回一个字符串数组:

 {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";
        }

然后我想按如下方式打印答案:

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

希望得到类似的东西:

您选择了以下选项: • option1 • option2 • option3

相反,我得到的是这样的:

• 选项 1 ,• 选项 2 ,• 选项 3

任何人都知道如何删除那些烦人的逗号?

4

1 回答 1

1

您正在将一个数组连接到一个字符串,JS 通过调用Array#join参数来处理这个问题,。所以考虑到这一点,你可以做let foods = answers.food ? "You chose the following: \n " + answers.food.map(option => "• " + option + "\n").join('') : "";

于 2020-01-12T23:44:39.643 回答