我有一系列药物代码'BARB', 'HALU', 'METH'
。我想循环遍历这些代码并为每个代码创建一个新对象。
var saveObj = {};
saveObj.SelectedList = [];
angular.forEach(self.DrugNamesSelected, function(value, key) {
//Load Objects into array
var newObj = {};
newObj = self.Selected;
newObj.FK_CLNT_PERSFK_PER = $routeParams.personId;
newObj.DRUG_TYP_CODE = value;
saveObj.SelectedList.push(newObj);
});
这就是我得到的
saveObj.SelectedList [0] .DRUG_TYP_CODE ='METH' saveObj.SelectedList [1] .DRUG_TYP_CODE ='METH' saveObj.SelectedList [2] .DRUG_TYP_CODE ='METH'
这就是我需要的
saveObj.SelectedList [0] .DRUG_TYP_CODE ='BARB' saveObj.SelectedList [1] .DRUG_TYP_CODE ='HALU' saveObj.SelectedList [2] .DRUG_TYP_CODE ='METH'
答案 0 :(得分:3)
好像An unhandled exception occurred while processing the request.
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
GLibrary.Controllers.SecurityController.Login (GLibrary)
PLibrary.Controllers.SecurityController.Login (PLibrary)
让你有问题。所以发生的事情是每个元素都存储newObj = self.Selected;
的引用(即所有对象都指单个内存引用)。因此,当最后一个值更新为self.Selected
时,所有3个元素都具有相同的METH
值。
self.Selected
如果您出于某种原因需要该特定对象分配,我建议您使用angular.forEach(self.DrugNamesSelected, function(value, key) {
//Load Objects into array
var newObj = {};
//newObj = self.Selected; //removed this line
newObj.FK_CLNT_PERSFK_PER = $routeParams.personId;
newObj.DRUG_TYP_CODE = value;
saveObj.SelectedList.push(newObj);
});
将Object.assign
属性复制到self.Selected
。
newObj
IE11不支持Object.assign(newObj, self.Selected)
开箱即用。您可以考虑从here