鍍金池/ 問答/Python  HTML/ ES6關于數組循環(huán)的問題

ES6關于數組循環(huán)的問題

res.data.list 的數據 [{…}, {…}, {…}] 是數組里套json的形式

我想把res.data.list 循環(huán)放到一個數組里 (因為要請求很多次數據,把每次返回的數據放在一個數組里處理)
但是輸出的數組顯示的是這樣的
[__ob__: Observer]
{…}
{…}
{…}
{…}
length:4
__ob__:Observer {value: Array(4), dep: Dep, vmCount: 0}
__proto__:Array

typeof 是object 沒法用數組的方法

that.diviCot=[];//要輸出的數組                
        for(var i in this.otherCat){
                getDivinationCont(this.otherCat[i].id).then(function(res){ //從后臺取數據 
                            if(res.code<10000){
                                console.log(res.data.list)// 返回的數據
                                    for(var t in res.data.list){//循環(huán)返回的數據                
                                        that.diviCot.push(res.data.list[t]); //放進聲明的數組里
                                    };
                            };        
                        })
                    };

for in 和for of 都試了
實在是不知道了。。

回答
編輯回答
菊外人

for in會把[__ob__: Observer]當成一個子項進行遍歷,所以res.data.list長度不是4而是5。
解決方法:
1、for in 里面過濾掉:

for(let i in obj){
    if(typeof obj[i] == "undefined") return;
}

2、不要使用for in 方法,改為:

for(let i = 0 ; i < obj.length; i++){
   
}
2017年7月13日 19:06
編輯回答
陌離殤

代碼沒有錯,輸出也沒有錯啊,
__ob__: Observer這些數據是vue這個框架對數據設置的監(jiān)控器,一般都是不可枚舉的。也就說不會影響你數據的使用

如果你想代碼簡單點的話,可以用

that.diviCot = [...that.diviCot, ...res.data.list]

that.diviCot=[];//要輸出的數組                
        for(var i in this.otherCat){
                getDivinationCont(this.otherCat[i].id).then(function(res){ //從后臺取數據 
                            if(res.code<10000){
                                console.log(res.data.list)// 返回的數據
                                 that.diviCot = [...that.diviCot, ...res.data.list]  
                            };        
                        })
                    };
2017年3月31日 11:57
編輯回答
亮瞎她

樓主解決了嗎

2017年11月5日 11:14