Javascript跳过循环
我正在使用JS和Javascript跳过循环,javascript,arrays,command-line-interface,inquirer,Javascript,Arrays,Command Line Interface,Inquirer,我正在使用JS和inquirer编写一个CLI应用程序。从下面的代码中,我希望从两个for循环中获得一些console.log()。但是,即使checkChoices和answers中都有值,也没有console.log JS view(){ let checkChoices = []; for(let i = 0; i < this.list.length; i++){ checkChoices.push(this.list[i].
inquirer
编写一个CLI应用程序。从下面的代码中,我希望从两个for循环中获得一些console.log()。但是,即使checkChoices
和answers
中都有值,也没有console.log
JS
view(){
let checkChoices = [];
for(let i = 0; i < this.list.length; i++){
checkChoices.push(this.list[i].text);
}
let viewList = [
{
type: 'checkbox',
name: 'command',
message: 'Your Checklist',
choices: checkChoices
}
]
console.log(checkChoices.length);
inquirer.prompt(viewList).then((answers) => {
for(let i = 0; i < answers.length; i++){
let answer = answers[i];
console.log("answer", answer)
for(let j = 0; j < checkChoices.length; j++){
console.log("choice", checkChoices[j])
if(answer == checkChoices[j]){
this.list[j].complete = true;
}
}
}
console.log(this.list);
// this.ask();
})
}
};
view(){
让checkChoices=[];
for(设i=0;i{
for(设i=0;i
通过注释,发现答案
是一个对象,而不是数组。对象本身没有length
属性
由于对象只有一个属性
command
,其值为数组,因此假设逻辑应该在answers.command
上循环,而不是试图在answers对象上循环。通过注释,发现answers
是一个对象,而不是数组。对象本身没有length
属性
由于对象只有一个属性
command
,其值为数组,因此假设逻辑应该循环answers.command
,而不是尝试在answers对象上循环。在您的第一个for
之前放置一个控制台日志,并验证的成功部分,然后检查是否为偶数happening@Taplar我把console.log放在第一个for循环之前,它的输出非常好。好吧,如果你console.log(答案)
它看起来像什么?@Taplar看起来像这样:{command:['hello']}好的,这是一个对象。对象没有长度。您希望对阵列使用answers.command
,在第一次for
之前立即输出控制台日志,并验证的成功部分,然后检查是否为偶数happening@Taplar我把console.log放在第一个for循环之前,它的输出非常好。好吧,如果你console.log(答案)
它看起来像什么?@Taplar看起来像这样:{command:['hello']}好的,这是一个对象。对象没有长度。您想对数组使用answers.command