如何再次创建动态变量和求和值以分配动态变量?
例如,
var incomeCr = 0, incomeDr = 0;
for (var j = 1; j <= noOfMonths; j++)
{
if(response[i]["AmountType" + j] == "Cr")
{
if(response[i]["ClosingBalanceWithType" + j] < 0)
{
response[i]["ClosingBalanceWithType" + j] = -1 * response[i]["ClosingBalanceWithType" + j];
}
incomeCr += parseFloat(response[i]["ClosingBalanceWithType" + j]);
}
else if (response[i]["AmountType" + j] == "Dr")
{
if (response[i]["ClosingBalanceWithType" + j] < 0)
{
response[i]["ClosingBalanceWithType" + j] = -1 * response[i]["ClosingBalanceWithType" + j];
}
incomeDr += parseFloat(response[i]["ClosingBalanceWithType" + j]);
}
}
这里如何创建动态变量incomeCr1,incomeCr2 ...和incomeDr1,incomeDr2 ......并将值再次赋值给动态变量。
答案 0 :(得分:0)
您不能像这样创建动态变量。而是使用数组:
var incomeCr = [];
//incomeCr[1], incomeCr[2],...
incomeCr[1] = "txt";
incomeCr[2] = "txt2";
答案 1 :(得分:0)
var incomeCr = 0, incomeDr = 0, expenseCr = 0, expenseDr = 0, iIncomeCr = {}, eExpenseCr = {}, iIncomeDr = {}, eExpenseDr = {}, cr = [];
for (var i = 0; i < response.length; i++) {
// Sum Primary Group Income on each Month
if (response[i].Type == "Ledger" && response[i].PrimaryGroup == "Incomes") {
for (var j = 1; j <= noOfMonths; j++) {
if (response[i]["AmountType" + j] == "Cr") {
if (response[i]["ClosingBalanceWithType" + j] < 0) {
response[i]["ClosingBalanceWithType" + j] = -1 * response[i]["ClosingBalanceWithType" + j];
}
iIncomeCr["incomeCr" + j] = (iIncomeCr["incomeCr" + j] ? iIncomeCr["incomeCr" + j] : 0) + parseFloat(response[i]["ClosingBalanceWithType" + j]);
iIncomeDr["incomeDr" + j] = iIncomeDr["incomeDr" + j] ? iIncomeDr["incomeDr" + j] : 0;
} else if (response[i]["AmountType" + j] == "Dr") {
if (response[i]["ClosingBalanceWithType" + j] < 0) {
response[i]["ClosingBalanceWithType" + j] = -1 * response[i]["ClosingBalanceWithType" + j];
}
iIncomeDr["incomeDr" + j] = (iIncomeDr["incomeDr" + j] ? iIncomeDr["incomeDr" + j] : 0) + parseFloat(response[i]["ClosingBalanceWithType" + j]);
iIncomeCr["incomeCr" + j] = iIncomeCr["incomeCr" + j] ? iIncomeCr["incomeCr" + j] : 0;
}
}
}