如何生成变量并动态分配内部for循环?
例如,
var tempClosingBalance, k = 1;
for (var i = 0; i < customArray.length; i++) {
tempClosingBalance = "ClosingBalanceWithType" + k;
for (var j = 0; j < output.length; j++) {
output[j].tempClosingBalance = customArray[i][j].ClosingBalanceWithType;
}
k++;
}
此处tempClosingBalance
变量包含ClosingBalanceWithType1
并分配值,相同条件继续在ClosingBalanceWithType2
,ClosingBalanceWithType3
中分配值等。
答案 0 :(得分:0)
我认为以下内容可行。直接.
运算符不适用于此处。
使用output[j]["ClosingBalanceWithType" + k]
。
var tempClosingBalance, k = 1;
for (var i = 0; i < customArray.length; i++) {
tempClosingBalance = "ClosingBalanceWithType" + k;
for (var j = 0; j < output.length; j++) {
output[j][tempClosingBalance] = customArray[i][j].ClosingBalanceWithType;
}
k++;
}
答案 1 :(得分:0)
for (var i = 0; i < customArray.length; i++) {
for (var j = 0; j < output.length; j++) {
output[j]["ClosingBalanceWithType" + k] = customArray[i][j].ClosingBalanceWithType;
output[j]["TransactionCount" + k] = customArray[i][j].TransactionCount ? customArray[i][j].TransactionCount : 0;
output[j]["AmountType" + k] = customArray[i][j].AmountType;
}
k++;
}