在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Python  HTML/ ES6關(guān)于數(shù)組循環(huán)的問(wèn)題

ES6關(guān)于數(shù)組循環(huán)的問(wèn)題

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

我想把res.data.list 循環(huán)放到一個(gè)數(shù)組里 (因?yàn)橐?qǐng)求很多次數(shù)據(jù),把每次返回的數(shù)據(jù)放在一個(gè)數(shù)組里處理)
但是輸出的數(shù)組顯示的是這樣的
[__ob__: Observer]
{…}
{…}
{…}
{…}
length:4
__ob__:Observer {value: Array(4), dep: Dep, vmCount: 0}
__proto__:Array

typeof 是object 沒(méi)法用數(shù)組的方法

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

for in 和for of 都試了
實(shí)在是不知道了。。

回答
編輯回答
菊外人

for in會(huì)把[__ob__: Observer]當(dāng)成一個(gè)子項(xiàng)進(jìn)行遍歷,所以res.data.list長(zhǎng)度不是4而是5。
解決方法:
1、for in 里面過(guò)濾掉:

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
編輯回答
陌離殤

代碼沒(méi)有錯(cuò),輸出也沒(méi)有錯(cuò)啊,
__ob__: Observer這些數(shù)據(jù)是vue這個(gè)框架對(duì)數(shù)據(jù)設(shè)置的監(jiān)控器,一般都是不可枚舉的。也就說(shuō)不會(huì)影響你數(shù)據(jù)的使用

如果你想代碼簡(jiǎn)單點(diǎn)的話,可以用

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

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

樓主解決了嗎

2017年11月5日 11:14