我很难编写代码的某些部分。我有一个detailData
数组,用于保存其他对象。
var detailData = [];
detailData[0] = {
"Detail1": null, "Detail2": null, "Detail3": null, "Detail4": null, "Detail5": null,
"RequestTrackingId1": null, "RequestTrackingId2": null, "RequestTrackingId3": null, "RequestTrackingId4": null,
"RequestTrackingId5": null, "header": "Request Tracking Id"
}
detailData[1] = {
"Detail1": null, "Detail2": null, "Detail3": null, "Detail4": null, "Detail5": null,
"RequestTrackingId1": null, "RequestTrackingId2": null, "RequestTrackingId3": null, "RequestTrackingId4": null,
"RequestTrackingId5": null, "header": "Source System Code"
}
detailData[2] = {
"Detail1": null, "Detail2": null, "Detail3": null, "Detail4": null, "Detail5": null,
"RequestTrackingId1": null, "RequestTrackingId2": null, "RequestTrackingId3": null, "RequestTrackingId4": null,
"RequestTrackingId5": null, "header": "Source System UId"
}
因此每个detailData对象都将包含detail1,detail2,detail3等。
现在从另一个数组我想为它们分配如下值:
for (var i = 0; i < $scope.arc_srcsys_cds.length; i++) {
var detailIndex = "Detail" + (i + 1);
detailData[1].detailIndex = $scope.arc_srcsys_cds[i];
}
但它没有为detail1,detail2
分配值,依此类推。而是创建另一个名为detailIndex
的字段。
如何达到预期效果?
答案 0 :(得分:3)
你可以试试这个:
detailData[i][detailIndex] = $scope.arc_srcsys_cds[i];
// detailData[1] will only replace or second array not for all
答案 1 :(得分:2)
尝试:
for (var i = 0; i < $scope.arc_srcsys_cds.length; i++) {
var detailIndex = "Detail" + (i + 1);
detailData[i][detailIndex] = $scope.arc_srcsys_cds[i];
}