从 Node Inquirer 的复选框创建的数组中删除逗号
Removing commas from Array created with checkbox from Node Inquirer
我有以下询问者提示,据我所知returns 一个字符串数组:
{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('') : "";
我有以下询问者提示,据我所知returns 一个字符串数组:
{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('') : "";