【问题标题】:switching javascript functions from foreach to for loops将 javascript 函数从 foreach 切换到 for 循环
【发布时间】:2012-08-12 19:18:48
【问题描述】:

我正在尝试修复我使用“forEach”构建的功能,该功能与应该过时的网络浏览器 (IE) 不兼容。我在 Javascript 中做到了这一点。我尝试将其转换为 for 循环,但没有成功。如果有人可以通过摆脱 foreach 并使用 for 循环来帮助我转换这两个函数,我将不胜感激。

这是他们引用的两个数组。

var yr1 = 2011, yr2 = 2012, yr3 = 2013, yr4 = 2014;
var years = [yr1, yr2, yr3, yr4];
//array with months and associated days
var calendar = [
    ["January", 31],["February", 28],["March", 31],["April", 30],["May", 31],["June", 30],["July", 31],["August", 31],["September", 30],
    ["October", 31],["November", 30],["December", 31]];

以下是需要从 forEach 更改为 for 循环的函数。

    //this creates the month values
function generateMonths() {
    var df = document.createDocumentFragment();
    calendar.forEach(function(info, i) {
        df.appendChild(createOption(info[0], i));
    });
        //clears past months
    clearChildren(sel_month);
        //appends new months onto variable df
    sel_month.appendChild(df);
}
//this creates the year values
function generateYears() {
    var df = document.createDocumentFragment();
    years.forEach(function(i) {
        df.appendChild(createYearOption(i));
    });
    //clears past months
    clearChildren(sel_year);
    //appends new months onto variable df
    sel_year.appendChild(df);
}

这是我失败的尝试,以证明我尝试过。

    //this creates the month values
function generateMonths() {
    var df = document.createDocumentFragment();
        for (var w = 0; w < 12; w++) {
            (function(calendar, w) {
                df.appendChild(createOption(calendar[0], w));
            });
        }
    //calendar.forEach(function(info, i) {
        //df.appendChild(createOption(info[0], i));
    };
        //clears past months
    clearChildren(sel_month);
        //appends new months onto variable df
    sel_month.appendChild(df);
}
    //this creates the year values
    function generateYears() {
    var df = document.createDocumentFragment();
        for (var w = 0; w < 12; w++) {
            (function(years) {
                df.appendChild(createOption(years[0]));
            });
        }        

        //years.forEach(function(i) {
        //df.appendChild(createYearOption(i));
    };
        //clears past months
    clearChildren(sel_year);
        //appends new months onto variable df
    sel_year.appendChild(df);
}

【问题讨论】:

    标签: javascript function loops for-loop foreach


    【解决方案1】:
    var years = [yr1, yr2, yr3, yr4];
    
    years.forEach(function(i) {
        df.appendChild(createYearOption(i));
    });
    

    相当于:

    for(var i=0; i<years.length; i++) {
      df.appendChild(createYearOption(years[i]));
    }
    

    我不确定您的forEach 在您的第二种情况下接受什么。但是我认为这就是你想要做的:

    var calendar = [["January", 31],["February", 28]];
    
    calendar.forEach(function(info, i) {
        df.appendChild(createOption(info[0], i));
    });
    

    而是使用循环来createOption 与每个数组的两个元素。

    for(var i=0; i<calendar.length; i++) {
      df.appendChild(createOption(calendar[i][0], calendar[i][1]));
    }
    

    【讨论】:

    • 另一个呢..对不起,我遇到了麻烦。
    【解决方案2】:

    如果您愿意,您可以随时填充 forEach。来自MDN

    if ( !Array.prototype.forEach ) {
      Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
          fn.call(scope || this, this[i], i, this);
        }
      }
    }
    

    【讨论】:

    • +1 乍一看,这似乎是提供兼容性的最简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多