>我有以下询问器提示,据我所知,它返回一个字符串数组:
{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") : "";
希望得到类似的东西:
您选择了以下内容: • 选项1 • 选项2 • 选项3
相反,我得到了这样的东西:
• 选项1 ,• 选项2 ,• 选项3
任何人都知道如何删除那些烦人的逗号?
您正在将数组连接到字符串,JS 通过使用参数,
调用Array#join
来处理这个问题。因此,考虑到这一点,您可以做let foods = answers.food ? "You chose the following: n " + answers.food.map(option => "• " + option + "n").join('') : "";