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

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

Nested loop is not returning value in Angular TypeScript

问题

  1. 私有函数checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) => {
  4. saleBlockList.map(saleBlock => {
  5. saleBlock.saleLineList.map(saleLineList => {
  6. console.log('saleLineList', saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &&
  9. saleLineList.deliveryNotes.findIndex(
  10. item => item.articleId === line.code && item.quantity >= line.missingQuantity
  11. ) >= 0
  12. );
  13. });
  14. });
  15. return false;
  16. })
  17. );
  18. }
英文:

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 -->

  1. private checkDeliveryNotesQuantity(line: SaleLine): Observable&lt;boolean&gt; {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) =&gt; {
  4. saleBlockList.map(saleBlock =&gt; {
  5. saleBlock.saleLineList.map(saleLineList =&gt; {
  6. console.log(&#39;saleLineList&#39;, saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &amp;&amp;
  9. saleLineList.deliveryNotes.findIndex(
  10. item =&gt; item.articleId === line.code &amp;&amp; item.quantity &gt;= line.missingQuantity
  11. ) &gt;= 0
  12. );
  13. });
  14. });
  15. return false;
  16. })
  17. );
  18. }

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

原始代码部分:

  1. private checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) => {
  4. return saleBlockList.map(saleBlock => {
  5. return saleBlock.saleLineList.map(saleLineList => {
  6. console.log('saleLineList', saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &&
  9. saleLineList.deliveryNotes.findIndex(
  10. item => item.articleId === line.code && item.quantity >= line.missingQuantity
  11. ) >= 0
  12. );
  13. });
  14. });
  15. })
  16. );
  17. }

建议修复部分:

  1. private checkDeliveryNotesQuantity(line: SaleLine): Observable<boolean> {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) => {
  4. return saleBlockList.some(saleBlock => {
  5. return saleBlock.saleLineList.some(saleLineList => {
  6. console.log('saleLineList', saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &&
  9. saleLineList.deliveryNotes.findIndex(
  10. item => item.articleId === line.code && item.quantity >= line.missingQuantity
  11. ) >= 0
  12. );
  13. });
  14. });
  15. })
  16. );
  17. }

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

英文:

Nested map lacks return:

  1. private checkDeliveryNotesQuantity(line: SaleLine): Observable&lt;boolean&gt; {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) =&gt; {
  4. return saleBlockList.map(saleBlock =&gt; {
  5. return saleBlock.saleLineList.map(saleLineList =&gt; {
  6. console.log(&#39;saleLineList&#39;, saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &amp;&amp;
  9. saleLineList.deliveryNotes.findIndex(
  10. item =&gt; item.articleId === line.code &amp;&amp; item.quantity &gt;= line.missingQuantity
  11. ) &gt;= 0
  12. );
  13. });
  14. });
  15. })
  16. );
  17. }

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

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

  1. private checkDeliveryNotesQuantity(line: SaleLine): Observable&lt;boolean&gt; {
  2. return this.posSalesTableFacade.getStoreSaleBlockList$().pipe(
  3. map((saleBlockList: SaleBlock[]) =&gt; {
  4. return saleBlockList.some(saleBlock =&gt; {
  5. return saleBlock.saleLineList.some(saleLineList =&gt; {
  6. console.log(&#39;saleLineList&#39;, saleLineList);
  7. return (
  8. saleLineList.deliveryNotes &amp;&amp;
  9. saleLineList.deliveryNotes.findIndex(
  10. item =&gt; item.articleId === line.code &amp;&amp; item.quantity &gt;= line.missingQuantity
  11. ) &gt;= 0
  12. );
  13. });
  14. });
  15. })
  16. );
  17. }

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:

确定