mardi 4 juin 2019

Else Statement Does Not Stop Looping NodeJS

I have been working on this code to read through a PDF file and grab the keywords of company names and display them. It all works fine except for one part where the else if statement outputs one line (which is what I want) but the else statement that comes last, which is supposed to output "Not Found" loops 20 times where I only want it to display the output only once instead of 20 times.

I have tried numerous ways by going through the internet to change my code, most recommended that forEach is not a proper way to do things and that I should use for instead but when I do, I just can't seem to get it right.

l.forEach(function(element) {

    var j = element['fullTextAnnotation']['text'];   
    var sd = 'SDN. BHD.';
    var bd = 'BHD.';
    var et = 'Enterprise'; 
    var inc = 'Incorporated'; 

    var regtoken = new natural.RegexpTokenizer({pattern:/\n/});
    var f = regtoken.tokenize(jsondata);   

for(o = 0 ; o < f.length; o++){
    var arrayline1 = natural.LevenshteinDistance(sd,f[o],{search:true});
    var arrayline2 = natural.LevenshteinDistance(bd,f[o],{search:true});
    var arrayline3 = natural.LevenshteinDistance(et,f[o],{search:true});
    var arrayline4 = natural.LevenshteinDistance(inc,f[o],{search:true});
    var arrayline5 = natural.LevenshteinDistance(nf,f[o],{search:false});

    var onedata1 = arrayline1['substring'];
    var onedata2 = arrayline2['substring'];
    var onedata3 = arrayline3['substring'];
    var onedata4 = arrayline4['substring'];
    var onedata5 = arrayline5['substring'];

    if (onedata1 === sd)
    {
        tokends = f[o];            
        break;
    } else if(onedata3 === et)

    {
        tokends = f[o];
        break;

    } else if(onedata2 === bd) 

    {
        tokends = f[o];
        console.log(tokends);
        break;

    } else if(onedata4 === inc)

    {
        tokends = f[o];
        console.log(tokends);
        break;

    } else{

       console.log("Not Found");
       return false;    
    }  

}

}); 

I wish to get only one "Not Found" output for the else statement rather than it looping it for 20 times over. Hopefully I could get some insight to this problem. Thank you.

Aucun commentaire:

Enregistrer un commentaire