typescript 如何推送来自多个连续调用的结果

inn6fuwd  于 2023-05-19  发布在  TypeScript
关注(0)|答案(1)|浏览(135)

我试图得到所有的响应,并将其放入数组中。在我得到所有的回应后,我想把它们展示在视野中
为此,我开发了这个函数:
对于每一个值,我做不同的调用:
“listaCodiciPanFlotta”:[ 92994906,92994940,92994907,92994941,92994908,92994910 ]

dettaglioPan.forEach(x => {
                   let codicePans = x.listaCodiciPanFlotta;
                       if (codicePans.length > 0) {
                           codicePans.forEach(value => {
                           this.getCodicePanList(value);})
                                }
                            })


getCodicePanList(value) {
    let codicePanList = [];
    this.dettaglioPanService.findDettaglioCodicePanList('pan', 'dettaglioPan',
        [
            { name: 'codicePan', value: value },
        ])
        .subscribe(dettaglioCodicePan => {
            this.listaCodiciPanFlottaStatus = true;
            codicePanList.push(dettaglioCodicePan)
            this.dettaglioCodicePanData = codicePanList;
        })
}

这里的结果是:仅推送最后的响应。我想有一个数组,其中包含根据列表代码收到的所有答案。所以列表有6个代码,我希望数组中有6个不同的答案。
谢谢你的回答。

ukqbszuj

ukqbszuj1#

每次你创建一个新的数组,并将一个项目推到它。相反,你应该已经创建了一次数组,并将所有项目推送到它。就像这样:

this.dettaglioCodicePanData = [];
dettaglioPan.forEach(x => {
                       let codicePans = x.listaCodiciPanFlotta;
                           if (codicePans.length > 0) {
                               codicePans.forEach(value => {
                               this.getCodicePanList(value);})
                                    }
                                })
    
    
    
    getCodicePanList(value) {
        let codicePanList = [];
        this.dettaglioPanService.findDettaglioCodicePanList('pan', 'dettaglioPan',
            [
                { name: 'codicePan', value: value },
            ])
            .subscribe(dettaglioCodicePan => {
                this.listaCodiciPanFlottaStatus = true;
                this.dettaglioCodicePanData.push(dettaglioCodicePan);
            })
    }

在这里,不是将具有一个元素的数组分配给dettaglioCodicePanData,而是直接将所有元素推送给它

相关问题