code show as below:
angular.forEach(data.body.data.assets,function(val,ind){
angular.forEach(val.userAssetList,function(data,index){
val.userDtoName = data;
// alert(data.userDTO);
if(val.assetCount != val.availableCount){
lay.push(val);
}
})
})
最后我的lay數(shù)組中userDtoName的值全部都是最后一個data的值。為什么。但是每一個循環(huán)中alert出來的是不同的。
歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~
val is an object, equivalent to a pointer, so the same object is pushed in every time
Since I don’t know what other attributes and functions your val has, here is only an alternative: push({...val, userDtoName: data})
Using ES6’s... spread operator.
If you read it correctly, what you use conveniently is val.userAssetList, but you put val into lay. It’s the same every time, and you always execute lay.push(val)