Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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将类似的变量重构为forloop_Javascript_Variables_Pass By Reference - Fatal编程技术网

Javascript将类似的变量重构为forloop

Javascript将类似的变量重构为forloop,javascript,variables,pass-by-reference,Javascript,Variables,Pass By Reference,我试图简化代码,在这里我定义了许多具有相似结构的变量。i、 e: 要简化: var monthActual = document.createElement('input'); monthActual.name = "monthActual_" + showrooms[i]; monthActual.value = monthActualData; fullForm.appendChild(monthActual); var monthTarget = document.createEleme

我试图简化代码,在这里我定义了许多具有相似结构的变量。i、 e:

要简化:

var monthActual = document.createElement('input');
monthActual.name = "monthActual_" + showrooms[i];
monthActual.value = monthActualData;
fullForm.appendChild(monthActual);

var monthTarget = document.createElement('input');
monthTarget.name = "monthTarget_" + showrooms[i];
monthTarget.value = monthTargetData;
fullForm.appendChild(monthTarget);

var priorYear = document.createElement('input');
priorYear.name = "priorYear_" + showrooms[i];
priorYear.value = priorYearData;
fullForm.appendChild(priorYear);

var priorYearToDate = document.createElement('input');
priorYearToDate.name = "priorYearToDate_" + showrooms[i];
priorYearToDate.value = priorYearToDateData;
fullForm.appendChild(priorYearToDate);

var yearToDateTarget = document.createElement('input');
yearToDateTarget.name = "yearToDateTarget_" + showrooms[i];
yearToDateTarget.value = yearToDateTargetData;
fullForm.appendChild(yearToDateTarget);

var yearToDateActual = document.createElement('input');
yearToDateActual.name = "yearToDateActual_" + showrooms[i];
yearToDateActual.value = yearToDateActualData;
fullForm.appendChild(yearToDateActual);

var YTDVsPYTDSalesCurrency = document.createElement('input');
YTDVsPYTDSalesCurrency.name = "YTDVsPYTDSalesCurrency_" + showrooms[i];
YTDVsPYTDSalesCurrency.value = YTDVsPYTDSalesCurrencyData;
fullForm.appendChild(YTDVsPYTDSalesCurrency);

var YTDVsPYTDSalesinPercent = document.createElement('input');
YTDVsPYTDSalesinPercent.name = "YTDVsPYTDSalesinPercent_" + showrooms[i];
YTDVsPYTDSalesinPercent.value = YTDVsPYTDSalesinPercentData;
fullForm.appendChild(YTDVsPYTDSalesinPercent);

var YTDVsYTDTargetinSalesCurrency = document.createElement('input');
YTDVsYTDTargetinSalesCurrency.name = "YTDVsYTDTargetinSalesCurrency_" + showrooms[i];
YTDVsYTDTargetinSalesCurrency.value = YTDVsYTDTargetinSalesCurrencyData;
fullForm.appendChild(YTDVsYTDTargetinSalesCurrency);

var YTDVsYTDTargetinPercent = document.createElement('input');
YTDVsYTDTargetinPercent.name = "YTDVsYTDTargetinPercent_" + showrooms[i];
YTDVsYTDTargetinPercent.value = YTDVsYTDTargetinPercentData;
fullForm.appendChild(YTDVsYTDTargetinPercent);
var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
    'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];

for(var j=0; j<tableColumnData.length; j++){
    var temp = document.createElement('input');
    temp.name = tableColumnData[j]+ "_" + showrooms[i];
    temp.value = (tableColumnData[j] +"Data");
    fullForm.appendChild(temp);
}
var fullForm = document.createElement('form');
fullForm.action = '/fpdf/requests/print_fullreport.php?year=' + requestYear + '&month=' + getMonth(requestMonth);
fullForm.id = 'fullForm';
fullForm.target = '_blank';
fullForm.method = 'post';

var showrooms = [1, 3, 4, 24, 27, 29, 34, 36, 37, 8, 21, 25, 26, 28, 31, 33, -1];

for (var i = 0; i <showrooms.length; i++){
    var showroomData = allTargetsData.monthlyDetail[showrooms[i]];

    var currencyData = showroomData.currency;
    var showroomname = showroomData.showroom_name;

    var monthActualData = showroomData.total;
    var monthTargetData = Math.round(allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth]['amount']);
    var priorYearData = allTargetsData.realFigure[requestYear - 1][showrooms[i]]['figures'][requestMonth];
    var priorYearToDateData = showroomData.ly_ytd;
    var yearToDateTargetData = Math.round(showroomData.ytd_target);
    var yearToDateActualData = showroomData.ytd;

    var calculation1 = (showroomData.ytd - showroomData.ly_ytd).toFixed(2);
    var YTDVsPYTDSalesCurrencyData = calculation1;
    var calculation2 = (parseFloat(showroomData.ytd - showroomData.ly_ytd)/showroomData.ly_ytd).toFixed(2);
    var YTDVsPYTDSalesinPercentData = (calculation2*100);
    if (isNaN(YTDVsPYTDSalesinPercentData) || YTDVsPYTDSalesinPercentData == "Infinity"){
        YTDVsPYTDSalesinPercentData = 0;
    }
    var calculation3 = (showroomData.ytd - showroomData.ytd_target).toFixed(2);
    var YTDVsYTDTargetinSalesCurrencyData = (calculation3*100)/100;
    var calculation4 = (parseFloat(showroomData.ytd - showroomData.ytd_target)/parseFloat(showroomData.ytd_target)).toFixed(2);
    var YTDVsYTDTargetinPercentData = calculation4*100;
    if (isNaN(YTDVsYTDTargetinPercentData) || YTDVsYTDTargetinPercentData == "Infinity"){
        YTDVsYTDTargetinPercentData = 0;
    }


    var showroomCurrency = document.createElement('input');
    showroomCurrency.name = "showroomCurrency_" + showrooms[i];
    showroomCurrency.value = currencyData;
    fullForm.appendChild(showroomCurrency);

    var showroomNameField = document.createElement('input');
    showroomNameField.name = "showroomname_" + showrooms[i];
    showroomNameField.value = showroomname;
    fullForm.appendChild(showroomNameField);

    var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
        'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];

    for(var j=0; j<tableColumnData.length; j++){
        var temp = document.createElement('input');
        temp.name = tableColumnData[j]+ "_" + showrooms[i];
        temp.value = (tableColumnData[j] +"Data");
        fullForm.appendChild(temp);
    }
 }
我试图通过将变量放入数组,然后像这样迭代来简化它:

尝试简化:

var monthActual = document.createElement('input');
monthActual.name = "monthActual_" + showrooms[i];
monthActual.value = monthActualData;
fullForm.appendChild(monthActual);

var monthTarget = document.createElement('input');
monthTarget.name = "monthTarget_" + showrooms[i];
monthTarget.value = monthTargetData;
fullForm.appendChild(monthTarget);

var priorYear = document.createElement('input');
priorYear.name = "priorYear_" + showrooms[i];
priorYear.value = priorYearData;
fullForm.appendChild(priorYear);

var priorYearToDate = document.createElement('input');
priorYearToDate.name = "priorYearToDate_" + showrooms[i];
priorYearToDate.value = priorYearToDateData;
fullForm.appendChild(priorYearToDate);

var yearToDateTarget = document.createElement('input');
yearToDateTarget.name = "yearToDateTarget_" + showrooms[i];
yearToDateTarget.value = yearToDateTargetData;
fullForm.appendChild(yearToDateTarget);

var yearToDateActual = document.createElement('input');
yearToDateActual.name = "yearToDateActual_" + showrooms[i];
yearToDateActual.value = yearToDateActualData;
fullForm.appendChild(yearToDateActual);

var YTDVsPYTDSalesCurrency = document.createElement('input');
YTDVsPYTDSalesCurrency.name = "YTDVsPYTDSalesCurrency_" + showrooms[i];
YTDVsPYTDSalesCurrency.value = YTDVsPYTDSalesCurrencyData;
fullForm.appendChild(YTDVsPYTDSalesCurrency);

var YTDVsPYTDSalesinPercent = document.createElement('input');
YTDVsPYTDSalesinPercent.name = "YTDVsPYTDSalesinPercent_" + showrooms[i];
YTDVsPYTDSalesinPercent.value = YTDVsPYTDSalesinPercentData;
fullForm.appendChild(YTDVsPYTDSalesinPercent);

var YTDVsYTDTargetinSalesCurrency = document.createElement('input');
YTDVsYTDTargetinSalesCurrency.name = "YTDVsYTDTargetinSalesCurrency_" + showrooms[i];
YTDVsYTDTargetinSalesCurrency.value = YTDVsYTDTargetinSalesCurrencyData;
fullForm.appendChild(YTDVsYTDTargetinSalesCurrency);

var YTDVsYTDTargetinPercent = document.createElement('input');
YTDVsYTDTargetinPercent.name = "YTDVsYTDTargetinPercent_" + showrooms[i];
YTDVsYTDTargetinPercent.value = YTDVsYTDTargetinPercentData;
fullForm.appendChild(YTDVsYTDTargetinPercent);
var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
    'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];

for(var j=0; j<tableColumnData.length; j++){
    var temp = document.createElement('input');
    temp.name = tableColumnData[j]+ "_" + showrooms[i];
    temp.value = (tableColumnData[j] +"Data");
    fullForm.appendChild(temp);
}
var fullForm = document.createElement('form');
fullForm.action = '/fpdf/requests/print_fullreport.php?year=' + requestYear + '&month=' + getMonth(requestMonth);
fullForm.id = 'fullForm';
fullForm.target = '_blank';
fullForm.method = 'post';

var showrooms = [1, 3, 4, 24, 27, 29, 34, 36, 37, 8, 21, 25, 26, 28, 31, 33, -1];

for (var i = 0; i <showrooms.length; i++){
    var showroomData = allTargetsData.monthlyDetail[showrooms[i]];

    var currencyData = showroomData.currency;
    var showroomname = showroomData.showroom_name;

    var monthActualData = showroomData.total;
    var monthTargetData = Math.round(allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth]['amount']);
    var priorYearData = allTargetsData.realFigure[requestYear - 1][showrooms[i]]['figures'][requestMonth];
    var priorYearToDateData = showroomData.ly_ytd;
    var yearToDateTargetData = Math.round(showroomData.ytd_target);
    var yearToDateActualData = showroomData.ytd;

    var calculation1 = (showroomData.ytd - showroomData.ly_ytd).toFixed(2);
    var YTDVsPYTDSalesCurrencyData = calculation1;
    var calculation2 = (parseFloat(showroomData.ytd - showroomData.ly_ytd)/showroomData.ly_ytd).toFixed(2);
    var YTDVsPYTDSalesinPercentData = (calculation2*100);
    if (isNaN(YTDVsPYTDSalesinPercentData) || YTDVsPYTDSalesinPercentData == "Infinity"){
        YTDVsPYTDSalesinPercentData = 0;
    }
    var calculation3 = (showroomData.ytd - showroomData.ytd_target).toFixed(2);
    var YTDVsYTDTargetinSalesCurrencyData = (calculation3*100)/100;
    var calculation4 = (parseFloat(showroomData.ytd - showroomData.ytd_target)/parseFloat(showroomData.ytd_target)).toFixed(2);
    var YTDVsYTDTargetinPercentData = calculation4*100;
    if (isNaN(YTDVsYTDTargetinPercentData) || YTDVsYTDTargetinPercentData == "Infinity"){
        YTDVsYTDTargetinPercentData = 0;
    }


    var showroomCurrency = document.createElement('input');
    showroomCurrency.name = "showroomCurrency_" + showrooms[i];
    showroomCurrency.value = currencyData;
    fullForm.appendChild(showroomCurrency);

    var showroomNameField = document.createElement('input');
    showroomNameField.name = "showroomname_" + showrooms[i];
    showroomNameField.value = showroomname;
    fullForm.appendChild(showroomNameField);

    var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
        'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];

    for(var j=0; j<tableColumnData.length; j++){
        var temp = document.createElement('input');
        temp.name = tableColumnData[j]+ "_" + showrooms[i];
        temp.value = (tableColumnData[j] +"Data");
        fullForm.appendChild(temp);
    }
 }
var tableColumnData=['monthActual','monthTarget','priorYear','priorYearToDate','yearToDateTarget',
“yearToDateActual”、“YTDVsPYTDSalesCurrency”、“YTDVsPYTDSalesinPercent”、“YTDVsYTDTargetinSalesCurrency”、“YTDVsYTDTargetinPercent”];

对于(var j=0;j来说,您已经非常接近了,但只需要进行下一步。您需要将变量存储在数组或对象中,以便可以动态引用它们,而不是通过文本

在本例中,我使用了您的代码,并将所有额外数据放入
otherData
对象中。这样,您可以为这些数据指定人类可读的名称,但也可以使用方括号符号查找属性。即
otherData.monthActualData==otherData[“monthActualData”]

没有人承诺我是否做到了完美,但这个概念仍然存在

var fullForm=document.createElement('form');
fullForm.action='/fpdf/requests/print_fullreport.php?year='+requestYear+'&month='+getMonth(requestMonth);
fullForm.id='fullForm';
fullForm.target=''u blank';
fullForm.method='post';
var展厅=[1,3,4,24,27,29,34,36,37,8,21,25,26,28,31,33,-1];

对于(var i=0;i来说,您非常接近,但只需要采取下一步。您需要将变量存储在数组或对象中,以便可以动态引用它们,而不是通过文本

在本例中,我使用了您的代码,并将所有额外数据放入
otherData
对象中。这样,您可以为这些数据指定人类可读的名称,但也可以使用方括号符号查找属性。即
otherData.monthActualData==otherData[“monthActualData”]

没有人承诺我是否做到了完美,但这个概念仍然存在

var fullForm=document.createElement('form');
fullForm.action='/fpdf/requests/print_fullreport.php?year='+requestYear+'&month='+getMonth(requestMonth);
fullForm.id='fullForm';
fullForm.target=''u blank';
fullForm.method='post';
var展厅=[1,3,4,24,27,29,34,36,37,8,21,25,26,28,31,33,-1];

对于(var i=0;我在对象中存储对这些DOM对象的引用,并为属性赋予相同的名称,这是使用字符串引用特定变量的唯一方法。这可能更适合在对象中存储对这些DOM对象的引用,并为属性赋予相同的名称,这是引用特定变量的唯一方法可以使用字符串。这可能更适合