javascript - angular中controller里面雙層遍歷為何實(shí)現(xiàn)不了
問題描述
我需要實(shí)現(xiàn)這個(gè)功能,把獲得的數(shù)據(jù)進(jìn)行遍歷出一層,然后再對(duì)每一項(xiàng)遍歷,把每一項(xiàng)的數(shù)據(jù)的某個(gè)值賦予給上一層,但是卻實(shí)現(xiàn)不了。怎么辦?代碼: 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; 事實(shí)證明第二個(gè)循環(huán)里面的值alert可以出來。但是賦值給外層的元素就不行了。怎么解決。求大神。 數(shù)據(jù)結(jié)構(gòu)如下:我是想把userassetlist里面的數(shù)組進(jìn)行遍歷然后賦值給上一層。'data': {'assets': [ {'id': '001','assetCount': 14,'availableCount': 9,'price': 2,'startDate': null,'age': 0,'description': '','assetType': '臺(tái)式機(jī)','assetName': '賽爾','assetModel': 'Z-01','userAssetList': [ {'userDTO': { 'confirmationDate': null, 'firstJobDate': null, 'id': '', 'userCount': 0, 'orgList': [], 'roleList': [], 'salaryTypeList': [], 'socialSecurityList': [], 'failCount': 0, 'name': '管理員', 'personalEmail': '932276768@qq.com', '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': 'zdygx951@163.com', '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': '臺(tái)式機(jī)','assetName': '賽爾','assetModel': 'Z-01','userAssetList': [{ 'userDTO': {'confirmationDate': null,'firstJobDate': null,'id': '','userCount': 0,'orgList': [],'roleList': [],'salaryTypeList': [],'socialSecurityList': [],'failCount': 0,'name': '管理員','personalEmail': '932276768@qq.com','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': 'zdygx951@163.com','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]); }}
相關(guān)文章:
1. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時(shí)間會(huì)消失是什么情況?2. javascript - 在 vue里面用import引入js文件,結(jié)果為undefined3. python沒入門,請(qǐng)教一個(gè)問題4. php如何獲取訪問者路由器的mac地址5. html5 - input type=’file’ 上傳獲取的fileList對(duì)象怎么存儲(chǔ)于瀏覽器?6. 小程序怎么加外鏈,語句怎么寫!求救新手,開文檔沒發(fā)現(xiàn)7. 求教一個(gè)mysql建表分組索引問題8. node.js - 用nodejs 的node-xlsx模塊去讀取excel中的數(shù)據(jù),可是讀取出來的日期是數(shù)字,請(qǐng)問該如何讀取日期呢?9. sql語句如何按or排序取出記錄10. javascript - vue-resource中如何設(shè)置全局的timeout?
