重新启动/重用循环以迭代Javascript中具有不同参数的数组

重新启动/重用循环以迭代Javascript中具有不同参数的数组,javascript,arrays,loops,Javascript,Arrays,Loops,假设我有以下数组: const myQuestions = [ { question: ..., ... difficulty: 2, ... }, { question: ..., ... difficulty: 1 ... }, { question: ..., ... dif

假设我有以下数组:

const myQuestions = [
      { 
      question: ...,
         ...
      difficulty: 2,
        ...
    },
    {
      question: ...,
        ...
      difficulty: 1
        ...
      },
      {
        question: ...,
         ...
        difficulty: 3,
          ...
      }
      etc...
    ];
我想以这样一种方式迭代数组,我想选择发现的第一个有难度3的问题,这是这个数组中难度最高的问题

我像这样迭代数组:

var currentcount=0;
var tempcount=currentcount;
var currentdiff=myQuestions[currentcount].difficulty;
 for(tempcount;tempcount<myQuestions.length;tempcount++){
    if(currentdiff<=myQuestions[tempcount].difficulty){
            currentcount=tempcount;
            break;
          }
然后我想抓住下一个难度次高的问题,仍然是3,因为这是最高难度。但是,数组中已经没有难度3的问题了,所以下一个最高难度是2

我如何能够重用我的for循环(或任何其他循环),以便在所有难度2的问题都已用尽时,它使用查找难度2问题的新标准搜索同一数组,然后再搜索难度1的问题

我的猜测是,如果我到达数组的末尾但仍然没有找到任何内容,那么我需要更新currentdiff,然后在循环的末尾也将tempcount设置回0

到目前为止,我最好的猜测是这样的,但它并没有达到预期的效果:

var currentcount=0;
var tempcount=currentcount;

//keeps track of the current difficulty
var currentdiff=myQuestions[currentcount].difficulty;

        for(tempcount;tempcount<myQuestions.length;tempcount++){
         if(currentdiff<=myQuestions[tempcount].difficulty){
            currentcount=tempcount;
            break;  //stop the loop as the next question with the matching difficulty is found
          }
          else{
              //overallcount is the number of questions that have been grabbed and spliced so far
              //initially overallcount=0.
              //if not reached the end of the array, skip the rest of the statements and start the loop again from the next iteration
            if(overallcount<myQuestions.length){
              continue;
            }
            else if(currentdiff==3){
              currentdiff=2;
              tempcount=0;
            }
            else if(currentdiff==2){
              currentdiff=1;
              tempcount=0;
            }
            else{
             //if overallcount>=myQuestions.length, then the array is empty, so break the loop
              break;
            }
          }
        }
var currentcount=0;
var tempcount=当前计数;
//跟踪当前的困难
var currentdiff=myQuestions[currentcount]。难度;
对于(tempcount;tempcount,在循环这些问题之前,您可以选择这些问题,如下所示:

让我的问题=[{
问题:'',
难度:2,
},
{
问题:'',
难点:1,,
},
{
问题:'',
难度:3,
},
];
myQuestions.sort(函数(问题A、问题B){
返回问题B.难度-问题A.难度;
});

console.log(myQuestions);
我很难弄清楚您到底想做什么。似乎您只是希望能够以剩余的最大难度依次拉出元素?在这种情况下,为什么不简单地按该属性对数组进行
排序呢?然后您可以按顺序遍历它们。
var currentcount=0;
var tempcount=currentcount;

//keeps track of the current difficulty
var currentdiff=myQuestions[currentcount].difficulty;

        for(tempcount;tempcount<myQuestions.length;tempcount++){
         if(currentdiff<=myQuestions[tempcount].difficulty){
            currentcount=tempcount;
            break;  //stop the loop as the next question with the matching difficulty is found
          }
          else{
              //overallcount is the number of questions that have been grabbed and spliced so far
              //initially overallcount=0.
              //if not reached the end of the array, skip the rest of the statements and start the loop again from the next iteration
            if(overallcount<myQuestions.length){
              continue;
            }
            else if(currentdiff==3){
              currentdiff=2;
              tempcount=0;
            }
            else if(currentdiff==2){
              currentdiff=1;
              tempcount=0;
            }
            else{
             //if overallcount>=myQuestions.length, then the array is empty, so break the loop
              break;
            }
          }
        }