英文:
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<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;
})
);
}
<!-- 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<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
);
});
});
})
);
}
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<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
);
});
});
})
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论