我试图通过与使用数组表示法相反的对象表示法访问此javascript对象中的金额值。我将如何访问变量。
javascript对象名为: allTargetsData ,其中包含一个名为 originalTarget 的对象,该对象分为:年份,陈列室,月份,如图所示。
以下函数用于访问这些对象,但我对变量的对象表示法:monthTargetData语法错误,因为我试图从数组表示法转换它。
function printFullReport(){
var requestYear = $('#changeYear').val();
var requestMonth = parseInt($('#changeMonth').val());
var url = '/fpdf/requests/print_fullreport1.php?year=' + requestYear + '&month=' + getMonth(requestMonth);
console.log(allTargetsData);
//var showroomNo = 29;
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 lastYear = allTargetsData.realFigure.requestYear - 1;
var showroomname = showroomData.showroom_name;
var monthActualData = showroomData.total;
var monthTargetData = allTargetsData.originalTarget.requestYear.[showrooms[i]].requestMonth.amount;
var priorYearData = JSON.stringify(allTargetsData.realFigure[requestYear - 1][showrooms[i]]['figures'][requestMonth]);
var priorYearToDateData = showroomData.ly_ytd;
var yearToDateTargetData = showroomData.ytd_target;
var yearToDateActualData = showroomData.ytd;
var calculation1 = showroomData.ytd - showroomData.ly_ytd;
var YTDVsPYTDSalesCurrencyData = Math.round(calculation1*10000)/100;
var calculation2 = parseFloat(showroomData.ytd - showroomData.ly_ytd)/showroomData.ly_ytd;
var YTDVsPYTDSalesinPercentData = Math.round(calculation2*10000)/100;
var calculation3 = showroomData.ytd - showroomData.ytd_target;
var YTDVsYTDTargetinSalesCurrencyData = Math.round(calculation3*100)/100;
var calculation4 = parseFloat(showroomData.ytd - showroomData.ytd_target)/parseFloat(showroomData.ytd_target);
var YTDVsYTDTargetinPercentData = Math.round(calculation4*10000)/100;
console.log(showroomname);
}
}
我之前通过此声明访问它:
var monthTargetData = JSON.stringify(allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth]['amount']);
答案 0 :(得分:0)
您不能因使用变量来访问数据 - 这些不能与点表示法一起使用。关于该行,您唯一可以改变的是amount
。它可能是
allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth].amount
代码中的错误在于此处(不确定是否存在更多错误)
allTargetsData.originalTarget.requestYear.[showrooms[i]].requestMonth.amount;
^^^ dot and bracket notation doesn't work
我相信如此,因为括号表示将对象视为数组
不,不是,数组没有索引以外的(字符串)键。您永远不会使用something['string']