javascript - angular中controller里面雙層遍歷為何實現不了
問題描述
我需要實現這個功能,把獲得的數據進行遍歷出一層,然后再對每一項遍歷,把每一項的數據的某個值賦予給上一層,但是卻實現不了。怎么辦?代碼: var datalist=[];
var dataDe = data.body.data.assets;for(var i = 0;i<dataDe.length;i++){ for(var j= 0;j<dataDe[i].userAssetList.length;j++){dataDe[i].userDtoName = dataDe[i].userAssetList[j];datalist.push(dataDe[i]); }}$scope.datalist=datalist; 事實證明第二個循環里面的值alert可以出來。但是賦值給外層的元素就不行了。怎么解決。求大神。 數據結構如下:我是想把userassetlist里面的數組進行遍歷然后賦值給上一層。'data': {'assets': [ {'id': '001','assetCount': 14,'availableCount': 9,'price': 2,'startDate': null,'age': 0,'description': '','assetType': '臺式機','assetName': '賽爾','assetModel': 'Z-01','userAssetList': [ {'userDTO': { 'confirmationDate': null, 'firstJobDate': null, 'id': '', 'userCount': 0, 'orgList': [], 'roleList': [], 'salaryTypeList': [], 'socialSecurityList': [], 'failCount': 0, 'name': '管理員', 'personalEmail': '[email protected]', 'personalPhone': '', 'personalPhoneCountryCode': '86', 'lastSalary': 0, 'lastSalaryStartDate': null, 'currentSalary': 0, 'currentSalaryStartDate': null, 'birthDate': null, 'joiningDate': null, 'successCount': 0, 'locked': false, 'identified': true, 'active': false},'count': 3 }, {'userDTO': { 'confirmationDate': null, 'firstJobDate': null, 'id': '', 'userCount': 0, 'orgList': [], 'roleList': [], 'salaryTypeList': [], 'socialSecurityList': [], 'failCount': 0, 'name': '沙琪瑪', 'personalEmail': '[email protected]', 'personalPhone': '17328121234', 'personalPhoneCountryCode': '86', 'lastSalary': 0, 'lastSalaryStartDate': null, 'currentSalary': 0, 'currentSalaryStartDate': null, 'birthDate': null, 'joiningDate': null, 'successCount': 0, 'locked': false, 'identified': true, 'active': false},'count': 2 }],
問題解答
回答1:為什么不可以呢,你的代碼,一行沒改:
var data = { 'assets': [ {'id': '001','assetCount': 14,'availableCount': 9,'price': 2,'startDate': null,'age': 0,'description': '','assetType': '臺式機','assetName': '賽爾','assetModel': 'Z-01','userAssetList': [{ 'userDTO': {'confirmationDate': null,'firstJobDate': null,'id': '','userCount': 0,'orgList': [],'roleList': [],'salaryTypeList': [],'socialSecurityList': [],'failCount': 0,'name': '管理員','personalEmail': '[email protected]','personalPhone': '','personalPhoneCountryCode': '86','lastSalary': 0,'lastSalaryStartDate': null,'currentSalary': 0,'currentSalaryStartDate': null,'birthDate': null,'joiningDate': null,'successCount': 0,'locked': false,'identified': true,'active': false }, 'count': 3},{ 'userDTO': {'confirmationDate': null,'firstJobDate': null,'id': '','userCount': 0,'orgList': [],'roleList': [],'salaryTypeList': [],'socialSecurityList': [],'failCount': 0,'name': '沙琪瑪','personalEmail': '[email protected]','personalPhone': '17328121234','personalPhoneCountryCode': '86','lastSalary': 0,'lastSalaryStartDate': null,'currentSalary': 0,'currentSalaryStartDate': null,'birthDate': null,'joiningDate': null,'successCount': 0,'locked': false,'identified': true,'active': false }, 'count': 2}] } ]}var datalist=[];var dataDe = data.assets;for(var i = 0;i<dataDe.length;i++){ for(var j= 0;j<dataDe[i].userAssetList.length;j++){dataDe[i].userDtoName = dataDe[i].userAssetList[j];datalist.push(dataDe[i]); }}
相關文章:
1. android - NavigationView 的側滑菜單中如何保存新增項(通過程序添加)2. 微信小程序可以用gulp,webpack嗎?3. python - 為什么正常輸出中文沒有亂碼,zip函數之后出現中文編程unicode編碼的問題,我是遍歷輸出的啊。4. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?5. ueditor上傳服務器提示后端配置項沒有正常加載,求助!!!!!6. 提示語法錯誤語法錯誤: unexpected ’abstract’ (T_ABSTRACT)7. tp5 不同控制器中的變量調用問題8. 這段代碼既不提示錯誤也看不到結果,請老師明示錯在哪里,謝謝!9. php7.3.4中怎么開啟pdo驅動10. 老師 我是一個沒有學過php語言的準畢業生 我希望您能幫我一下
