Issue
I have a the below code, on which i am unable to break the loop on certain conditions.
isVoteTally(): boolean {
let count = false;
this.tab.committee.ratings.forEach(element => {
const _fo = this.isEmptyOrNull(element.ratings.finalOutcome.finaloutlook);
const _foreign = this.isEmptyOrNull(element.ratings.finalOutcome.foreign);
const _local = this.isEmptyOrNull(element.ratings.finalOutcome.local);
const _tally = element.ratings.finalOutcome.voteTally.maj + element.ratings.finalOutcome.voteTally.dis;
if (_fo == false && _foreign == false && _local == false) {
if (_tally > 0) {
**return count = false;**
}
} else {
if (_tally < 0) {
**return count = false;**
}
}
});
return count;
}
On the star-marked area, i want to break the code and return the boolean value, but i am unable to do it right now. Can anybody help me.
Thanks in advance.
Solution
It is not possible to break from forEach()
normally.
Alternatively you can use Array.every() because you wish to return false
while breaking the loop.
If you want to return true, then you can use Array.some()
this.tab.committee.ratings.every(element => {
const _fo = this.isEmptyOrNull(element.ratings.finalOutcome.finaloutlook);
const _foreign = this.isEmptyOrNull(element.ratings.finalOutcome.foreign);
const _local = this.isEmptyOrNull(element.ratings.finalOutcome.local);
const _tally = element.ratings.finalOutcome.voteTally.maj + element.ratings.finalOutcome.voteTally.dis;
if (_fo == false && _foreign == false && _local == false) {
if (_tally > 0) {
**return count = false;**
}
} else {
if (_tally < 0) {
**return count = false;**
}
}
});
Answered By - Amit Chigadani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.