需要一个会生成十六个变量的循环,以减少我的代码长度

时间:2018-04-13 13:21:47

标签: javascript loops

我有一个js表单赋值,我需要从表单创建一个字符串日期,并将其转换为不同的日期格式。我编写了代码(虽然它可能很难看),但我无法弄清楚如何编写一个有效的循环,所以我不必编写它创建的所有16个变量的代码。 - 变量是八个字符串日期的月份和年份变量,我已将其更改为“2015年6月”格式。

var dates = [enterDate,exitDate,enterDate2,exitDate2,enterDate3,exitDate3,enterDate4,exitDate4];
var months = ["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."];

var parts0 = dates[0].split('-');
var mydate0 = new Date(parts0[0], parts0[1] - 1, parts0[2]);
var year1 = mydate0.getFullYear();
var month1 = mydate0.getMonth();
month1 = months[month1];

var parts1 = dates[1].split('-');
var mydate1 = new Date(parts1[0], parts1[1] - 1, parts1[2]);
var year2 = mydate1.getFullYear();
var month2 = mydate1.getMonth();
month2 = months[month2];

var parts2 = dates[2].split('-');
var mydate2 = new Date(parts2[0], parts2[1] - 1, parts2[2]);
var year3 = mydate2.getFullYear();
var month3 = mydate2.getMonth();
month3 = months[month3];

var parts3 = dates[3].split('-');
var mydate3 = new Date(parts3[0], parts3[1] - 1, parts3[2]);
var year4 = mydate3.getFullYear();
var month4 = mydate3.getMonth();
month4 = months[month4];

var parts4 = dates[4].split('-');
var mydate4 = new Date(parts4[0], parts4[1] - 1, parts4[2]);
var year5 = mydate4.getFullYear();
var month5 = mydate4.getMonth();
month5 = months[month5];

var parts5 = dates[5].split('-');
var mydate5 = new Date(parts5[0], parts5[1] - 1, parts5[2]);
var year6 = mydate5.getFullYear();
var month6 = mydate5.getMonth();
month6 = months[month6];

var parts6 = dates[6].split('-');
var mydate6 = new Date(parts6[0], parts6[1] - 1, parts6[2]);
var year7 = mydate6.getFullYear();
var month7 = mydate6.getMonth();
month7 = months[month7];

var parts7 = dates[7].split('-');
var mydate7 = new Date(parts7[0], parts7[1] - 1, parts7[2]);
var year8 = mydate7.getFullYear();
var month8 = mydate7.getMonth();
month8 = months[month8];

=============================================== ====

我还没有进入循环。我还在试图理解地图。因此,如果我理解这一点,地图将遍历字符串日期数组并将它们转换为日期对象。所以datesObjArr现在应该包含八个日期对象。所以这不会起作用(但仍然没有循环):

var dates = [enterDate,exitDate,enterDate2,exitDate2,enterDate3,exitDate3,enterDate4,exitDate4];
var months = ["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."];


var dateObjArr  = dates.map( 
    function(s){
        var parts = s.split('-');
        return new Date(parts[0], parts[1] - 1, parts[2]);
    } );
}

var year1 = dateObjArr[0].getFullYear();
var month1 = dateObjArr[0].getMonth();
month1 = months[month1];

var year2 = dateObjArr[1].getFullYear();
var month2 = dateObjArr[1].getMonth();
month2 = months[month2];

var year3 = dateObjArr[2].getFullYear();
var month3 = dateObjArr[2].getMonth();
month3 = months[month3];

var year4 = dateObjArr[3].getFullYear();
var month4 = dateObjArr[3].getMonth();
month4 = months[month4];

var year5 = dateObjArr[4].getFullYear();
var month5 = dateObjArr[4].getMonth();
month5 = months[month5];

var year6 = dateObjArr[5].getFullYear();
var month6 = dateObjArr[5].getMonth();
month6 = months[month6];

var year7 = dateObjArr[6].getFullYear();
var month7 = dateObjArr[6].getMonth();
month7 = months[month7];

var year8 = dateObjArr[7].getFullYear();
var month8 = dateObjArr[7].getMonth();
month8 = months[month8];

1 个答案:

答案 0 :(得分:2)

我认为使用map并返回Date个对象

会更好。
var dateObjArr = dates.map( function(s){
    var parts = s.split('-');
    return new Date(parts[0], parts[1] - 1, parts[2]);
});

您可以迭代此数组并根据需要对其进行格式化。

或者您可以返回年份和月份(月份名称)本身的数组

var dateObjArr = dates.map( function(s){
    var parts = s.split('-');
    var date = new Date(parts[0], parts[1] - 1, parts[2]);
    return [ date.getFullYear() : months[date.getMonth()];
});