Javascript 是否有一种方法可以对提示的输入问题执行while循环,将它们绑定,并将所有答案存储在一个数组中?
我正在构建一个Yeoman生成器,它所需的依赖项来自和 这个想法是能够问用户一个问题,并重复相同的问题,即你想添加另一个。。。如果用户添加了另一个,那么它将绑定并记录该答案,如果用户说“否”或点击“返回”,则提示将停止 然后,我希望将所有答案绑定到一个数组,该数组可以传递给另一个对象函数,以便它可以将响应作为数组列出 这是到目前为止的代码。。。 首先是提示:Javascript 是否有一种方法可以对提示的输入问题执行while循环,将它们绑定,并将所有答案存储在一个数组中?,javascript,arrays,npm,yeoman,yeoman-generator,Javascript,Arrays,Npm,Yeoman,Yeoman Generator,我正在构建一个Yeoman生成器,它所需的依赖项来自和 这个想法是能够问用户一个问题,并重复相同的问题,即你想添加另一个。。。如果用户添加了另一个,那么它将绑定并记录该答案,如果用户说“否”或点击“返回”,则提示将停止 然后,我希望将所有答案绑定到一个数组,该数组可以传递给另一个对象函数,以便它可以将响应作为数组列出 这是到目前为止的代码。。。 首先是提示: askForTest1: function () { if (this.type == 'foundation5') {
askForTest1: function () {
if (this.type == 'foundation5') {
var cb = this.async();
var prompts = {
type: 'input',
name: 'test1',
message: chalk.yellow(' What is your favorite movie'),
default: 'Star Wars!'
};
this.prompt(prompts, function (props) {
this.templatedata.test1 = props.test1;
cb();
}.bind(this));
}
},
然后是copyTpl对象,它将绑定模板构建的选项:这是我希望出现的所需输出。。。请记住,此副本tpl与提示位于同一index.js文件中。i、 这个模板
this.fs.copyTpl(
this.templatePath('/index2.html'),
this.destinationPath('app/index2.html'),
{ title: [this.templatedata.test1-a, this.templatedata.test1-b, this.templatedata.test1-c, ...], h1: this.applicationName }
);
结果。。。包含此代码的模板
使用
将产生这个
using foo1
using foo2
这是可能的,我将如何着手做这件事 这是一项相当简单的编程任务 使用向用户提问的方法的递归。然后,如果用户回答“yes add more”,您只需再次调用相同的函数 大概是这样的:
initializing: function () {
this.movies = [];
},
askMovie: function (cb) {
cb = cb || this.async();
var prompts = [{
type: 'input',
name: 'movie',
message: chalk.yellow(' What is your favorite movie'),
default: 'Star Wars!'
}, {
type: 'confirm',
name: 'askAgain',
message: 'ask again?'
}];
this.prompt(prompts, function (props) {
this.movies.push(props.movie)
if (props.askAgain) {
this.askMovie(cb);
} else {
cb();
}
}.bind(this));
}
}
像这样的东西应该适合你:
function() {
var answers = {
times: [],
title: undefined,
type: undefined
};
function promptMe(prompt, cb) {
self.prompt(prompt, function (props) {
if(props.answer!= "done") {
answers.times.push(props.time);
promptMe(prompt, cb);
} else {
cb();
}
});
}
var cb = this.async(),
self = this,
movieTypePrompt = {
type: 'input',
name: 'movieType',
message: chalk.yellow('What is your favorite type of movie?'),
default: 'Action'
},
movieTitilePrompt = {
type: 'input',
name: 'movieTitle',
message: chalk.yellow('What is your favorite movie?'),
default: 'Tron: Legacy'
}
movieTimePrompt = {
type: 'input',
name: 'time',
message: chalk.yellow('When can you watch a movie? (enter \'done\' when you are done entering times)'),
default: '8PM'
};
//Prompt for movie type and title
this.prompt([movieTypePrompt, movieTitlePrompt], function (props) {
answers.title = props.movieTitle;
answers.type = props.movieType;
//Repeatedly prompt for movie times
promptMe(moviePrompt, function() {
console.log('done');
cb();
});
}.bind(this));
}
使用
array.push(value)
向数组添加值。这有助于写入DOM,但不适用于实际的dool.js文档。我怎么才能得到写入文档的值呢?我不知道什么是fool.js,但我不明白为什么它会有不同。您刚才问如何将结果放入数组中。当你完成的时候,你用阵列做什么并不重要。也许我完全没有正确地思考这个问题,但是这个阵列。推就是把它推到圆顶上。如果我想将它推送到一个实际的数组,该怎么办;foo.push(3)将3
推送到foo
变量中的数组上。我不知道这会以什么方式修改DOM。只是因为它是异步的,所以不太友好,也就是说,为什么没有使用while循环。。。这就是我最初的目标。我终于想出了类似的办法,但我真的很喜欢你的askAgain提示。。。这很有道理。这是一个非常普通的javascript/node.js模式。操作都是异步的,所以最好习惯回调。关于递归,这也是一个非常广泛的编程概念,你应该熟悉;它在很多情况下都很有用。