嵌套循环在Angular TypeScript中未返回值。

huangapple go评论68阅读模式
英文:

Nested loop is not returning value in Angular TypeScript

问题

私有函数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;
    })
  );
}
英文:

I have a nested loop in a function but it is returning the default value which is false, and not returning the value calculated inside the loop. Please let me know what I am doing wrong.

My code is given below. Thank you

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

原始代码部分:

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
            );
          });
        });
      })
    );
  }

建议修复部分:

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
            );
          });
        });
      })
    );
  }

请注意,修复后的 Observable 类型将为布尔类型。

英文:

Nested map lacks return:

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

Note, that the resulting Observable is going to be of type Array.

In order to fix that you should switch from map to some

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

huangapple
  • 本文由 发表于 2023年5月10日 23:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定