有没有办法避免在这段代码中有三个嵌套循环?

时间:2016-04-10 11:43:03

标签: javascript performance loops

我知道这个问题可能不属于这里,但如果有人愿意,我可以使用一些建议。

我想知道是否有办法以更有效的方式编写以下代码。

一般概念:

  1. 查看最多6个邻居测量列表的列表
  2. 检查列表是否包含任何非法组合(arfcnbsic的组合)
  3. 如果非法,请添加到警报列表中(如果此列表中尚不存在)。
  4. 以下是代码:

    var bcch_sacch = [70, 0, 0, 0, 0, 0]
    var combo = [[70, 30], [0, 0]]
    var alarm;
    var ncellcount = message.data.reports.numberofneighbors;
    
    //Loop through neighbors measurements (up to 6)
    for(var j = 0; j < 6; j++) {
    
        //If j is less than the amount of neighbors in the list (0-6)
        if(j < ncellcount) {
    
            //Combo: allowed combinations
            for(var i = 0; i < combo.length; i++) {
    
                //If allowed
                if(combo[i][0] == bcch_sacch[j] &&
                   combo[i][1] == message.data.reports.neighboringCells[j].BSIC) {
                }
                else {
                    for(var d = 0; d < alarm.length; d++) {
    
                        //If the current arfcn(j) and bsic(j) is in the alarms list
                        if(alarm[d][0] == bcch_sacch[j] &&
                           alarm[d][1] == message.data.reports.neighboringCells[j].BSIC) {
    
                        }
                        else {
                            //Add the combo in the alarm list
                            alarm[alarm.length][0] =  bcch_sacch[j];
                            alarm[alarm.length][1] = message.data.reports.neighboringCells[j].BSIC;
                        }
                    }
                }
            }
        }
    }
    
    //Print out the alarms list in a table
    for(var s = 0; s < alarm.length; s++) {
        $('#alarms tr:last').after('<tr><td>'+alarm[s][0]+'</td><td>'+alarm[s][1]+'</td></tr>');
    }
    

0 个答案:

没有答案