Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何显示循环数组中的元素_Javascript_Loops - Fatal编程技术网

Javascript 如何显示循环数组中的元素

Javascript 如何显示循环数组中的元素,javascript,loops,Javascript,Loops,我有一个数组,其中包含7个对象,问题如下: var questions = [ {question: 'Which turkish club did former Leeds player Harry Kewell join in 2008, which caused an uproar amongst Leeds supporters?'}, {question: 'Who is the former Liverpool star who beat Ruud Van

我有一个数组,其中包含7个对象,问题如下:

var questions = [ {question: 'Which turkish club did former Leeds player Harry Kewell join in 2008, which caused an uproar amongst Leeds supporters?'}, 
            {question: 'Who is the former Liverpool star who beat Ruud Van Nistelrooy\'s record of most prolific foreign goalscorer in their debut in the Premier League?'},
             {question: 'Who scored Liverpool\'s winner in \'that\' first 4-3 game against Kevin Keegan\'s Newcastle United in April 1996?'},
             {question: 'Which club was former Leeds Unted player Eric Cantona sold to in 1992?'},
             {question: 'Which former Aston Villa and Ireland midfielder went on to become a regular TV pundit with ITV?'},
             {question: 'How many European Cups had Liverpool won up to and including 2007-8?'},
             {question: 'Name the Liverpool scorers for the \'miracle of Istanbul\'.'}
];
我想在通过警报功能单击按钮时使用循环显示每个问题。我只需要显示数组中已经存在的问题。我不想向用户索要任何东西。因此,当单击按钮时,我只想拉出第一个元素并使用alert()显示,然后再次单击按钮时,我想显示第二个元素,直到通过单击按钮显示所有元素

例如,我尝试使用按钮的onlick属性和for循环来尝试将问题显示如下:

但是,这只需单击一下即可显示数组中的所有问题


谢谢

也许不是你想要的,但这是一个开始

var answers = questions.map(function(q) { return prompt(q.question); });
回答完所有问题后,您可以看到答案

console.log(answers);

最简单的方法是使用计数器:

var questions = ....
var counter = 0;
function onButtonClick(){
    alert(questions[counter]['question']);
}
您可以通过使用字符串数组而不是包含字符串的对象数组来简化事情

var questions = ['question1', 'question2'...];
var counter = 0;
function onButtonClick(){
    alert(questions[counter]);
}
如果希望用户回答您的问题,可以使用提示命令:

var questions = ['question1', 'question2'...];
var answers = new Array();
var counter = 0;
function onButtonClick(){
    answers.push(prompt(questions[counter++]['question']));
}

你不需要一个循环。您只需要一个变量来跟踪按钮被点击的次数(或者像@naomik提供的答案那样的神奇一行)

onClick()方法中:

var answer = prompt(questions[timesClicked]);  // ask user a question, store the answer
if(timesClicked < questions.length)
    timesClicked++;
else  // optionally you can reset the counter after all the questions have been answered
    timesClicked = 0;
var-answer=prompt(问题[timesClicked]);//向用户提问,存储答案
如果(点击时间<问题长度)
时间点++;
else//您可以在回答所有问题后重置计数器(可选)
时间=0;
请参见此

你可以用类似的东西做实验

const answers = [];

btn.addEventListener("click", function(){
  if(questions.length){
    answers.push(prompt(questions.pop().question));
  }
});

这是一个没有任何计数变量的紧凑解。除了
pop
,还可以使用
shift

编程中的“不确定”可以通过尝试和实验轻松弥补。您不需要使用循环。你只需要问问题[i],我是这个人点击按钮的次数。哇,这是使用
map
prompt
的好方法。
const answers = [];

btn.addEventListener("click", function(){
  if(questions.length){
    answers.push(prompt(questions.pop().question));
  }
});