javascript 嵌套循环在Angular TypeScript中不返回值

kpbwa7wx  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(93)

我在一个函数中有一个嵌套的循环,但它返回的默认值是false,而不是返回在循环中计算的值。请让我知道我做错了什么。
我的代码在下面。谢谢你

private checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
        return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
          map((saleBlockList: SaleBlock[]) => {
            saleBlockList.map(saleBlock => {
              saleBlock.saleLineList.map(saleLineList => {
                console.log('saleLineList', saleLineList);
                return (
                  saleLineList.deliveryNotes &&
                  saleLineList.deliveryNotes.findIndex(
                    item => item.articleId === line.code && item.quantity >= line.missingQuantity
                  ) >= 0
                );
              });
            });
            return false;
          })
        );
      }
zfciruhq

zfciruhq1#

嵌套Map缺少返回:

private checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
        return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
          map((saleBlockList: SaleBlock[]) => {
            return saleBlockList.map(saleBlock => {
              return saleBlock.saleLineList.map(saleLineList => {
                console.log('saleLineList', saleLineList);
                return (
                  saleLineList.deliveryNotes &&
                  saleLineList.deliveryNotes.findIndex(
                    item => item.articleId === line.code && item.quantity >= line.missingQuantity
                  ) >= 0
                );
              });
            });
          })
        );
      }

请注意,结果Observable将是Array类型。
为了解决这个问题,您应该从map切换到some

private checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
        return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
          map((saleBlockList: SaleBlock[]) => {
            return saleBlockList.some(saleBlock => {
              return saleBlock.saleLineList.some(saleLineList => {
                console.log('saleLineList', saleLineList);
                return (
                  saleLineList.deliveryNotes &&
                  saleLineList.deliveryNotes.findIndex(
                    item => item.articleId === line.code && item.quantity >= line.missingQuantity
                  ) >= 0
                );
              });
            });
          })
        );
      }

相关问题