I am using the below helper to iterate through a JSON array and return a result based on the condition to find whether an Account is closed or not (OpenOrClosedDesc=='Closed'
). I am getting all of the Accounts that are closed. But now I want to print an error message on the screen if there are no Closed Accounts.
Handlebars.registerHelper('each_Closed', function(list, opts) {
var i, result = '';
try {
//console.log("List Closed length "+ list.length)
for (i = 0; i < list.length; i++)
if (list[i].OpenOrClosedDesc == 'Closed'){
// console.log("List Closed "+ list[i].OpenOrClosedDesc == 'Closed')
result = result + opts.fn(list[i]);
}
return result;
}catch(e){
}
});
HTML Code :
<div id = "Revolving_ClosedAcc">
{{#repData}}
{{#each_Closed arf.TradeLine.TradeLine.[Revolving Accounts]}}
.
.
.
.
.
{{/each_Closed}}
{{/repData}}
</div>
EDIT Explanation for possible duplicate: I tried to return the error message from an else branch but it did not give accurate results. It simply shows there are no closed accounts if it finds one of the accounts is closed. It doesn't go through the for loop. The for loop is the main path as I want to iterate through the whole object array.
EDIT for having count of each account:
Handlebars.registerHelper('each_Closed', function(list, opts) {
var i, result = '',resCounter=0,closedAccountFound = false;
try {
//console.log("List Closed length "+ list.length)
for (i = 0; i < list.length; i++) {
if (list[i].OpenOrClosedDesc == 'Closed') {
// console.log("List Closed "+ list[i].OpenOrClosedDesc == 'Closed')
result = result + opts.fn(list[i]);
resCounter++;
closedAccountFound = true;
}
}
console.log(resCounter);
return closedAccountFound ? result : "No close account found.";
}catch(e){
}
});
I am getting count in console.log(resCounter) but how can i return that or in other words how i can print that in handlebars ? Do i have to write another helper ?
How can i do that?
Thanks in advance.