如何推送连续多次调用的结果

问题描述 投票:0回答:1

我正在尝试获得所有响应,并将其推送到一个数组中。收到所有回复后,我想让他们看到

为此我正在开发这个功能:

对于每个值我都在做不同的调用:

“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 个不同的答案。

谢谢你的回答。

arrays angular typescript push
1个回答
0
投票

每次您创建新数组并向其推送一个项目时。相反,您应该创建一次数组,然后将所有项目推送到其中。像这样:

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,而是直接将所有元素推送给它

© www.soinside.com 2019 - 2024. All rights reserved.