0

Inside nested for loop I am using if/else if else condition. When if and else if condition are false, final else condition is running as expected but as it is in for loop instead of just running once it is running multiple times. What changes do i need to make to make else condition work only once?

Here is my code

    productCodes: string[] = [],
    vehicleType: string[] = [],


      for (var i = 0; i < urls.length; i++) {
      return https.get(urls[i], res => {
        res.on('data', function(chunk) {
          json += chunk;
        });
        res.on('end', function() {
          var result = JSON.parse(json);

          for (var i = 0; i < result.length; i++) {
            for (var j = 0; j < result[i].products.length; j++) {

              if (productCodes.length !== 0 && productCodes !== undefined) {
                for (var k = 0; k < productCodes.length; k++) {
                  if (result[i].products[j].productCode == productCodes[k]) {
                    console.log(
                      'Product Codes: ' +
                        result[i].products[j].productCode
                    );
                  }
                }
              } else if (
                vehicleType.length !== 0 &&
                vehicleType !== undefined
              ) {
                for (var k = 0; k < vehicleType.length; k++) {
                  if (result[i].products[j].productType == vehicleType[k]) {
                    console.log(
                      'Product Codes: ' +
                        result[i].products[j].productCode 
                    );
                  }
                }

              } else {
                console.log('No data');
                break; ------------------------> HERE
              }

            }
          }
        });
      });
    }
    ```
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
OSAMA E
  • 341
  • 3
  • 15

1 Answers1

0

If the loops are to cease after you encounter your last "else" statement I would recommend breaking them, if necessary using labels.

Here is a related question! It's for java, but the syntax is similar.

If it is just about only displaying the message once, consider just setting a boolean, then asking for it after the loops have concluded, and if true log the message.

EDIT: To expand my answer for your edit, I am not sure what exactly your desired behaviour would be, but chances are you want to break a loop deeper down than the topmost one. break; will break the topmost loop, in your case I believe that would be

for (var j = 0; j < result[i].products.length; j++)

But not the loop directly above that, iterating over this loop all over again.

Try assigning a label to the loop you want to break further down, and then break that one specifically.

EDIT 2:

I've modified your code to include the example of labels. That way, you can break whichever loop you actually want to end. Just comment the appropriate break back in. Hope this is helpful!

productCodes: string[] = [],
    vehicleType: string[] = [],

    outer1:
    for (var i = 0; i < urls.length; i++)
    {
        return https.get(urls[i], res =>
        {
            res.on('data', function(chunk)
            {
                json += chunk;
            });
            res.on('end', function()
            {
                var result = JSON.parse(json);

                middle1:
                    for (var i = 0; i < result.length; i++)
                    {
                        for (var j = 0; j < result[i].products.length; j++)
                        {

                            if (productCodes.length !== 0 && productCodes !== undefined)
                            {
                                for (var k = 0; k < productCodes.length; k++)
                                {
                                    if (result[i].products[j].productCode == productCodes[k])
                                    {
                                        console.log(
                                            'Product Codes: ' +
                                            result[i].products[j].productCode
                                        );
                                    }
                                }
                            }
                            else if (
                                vehicleType.length !== 0 &&
                                vehicleType !== undefined
                            )
                            {
                                for (var k = 0; k < vehicleType.length; k++)
                                {
                                    if (result[i].products[j].productType == vehicleType[k])
                                    {
                                        console.log(
                                            'Product Codes: ' +
                                            result[i].products[j].productCode
                                        );
                                    }
                                }

                            }
                            else
                            {
                                console.log('No data');
                                //break outer1;
                                //break middle1;
                                //break;
                                -- -- -- -- -- -- -- -- -- -- -- -- > HERE
                            }

                        }
                    }
            });
        });
    }

As an additional note, nested labled loops or blocks like these are fairly uncommon due to the tendency to be able to resolve loops things in their own individual functions and then simply call those. I'd advise you to take a look at the java-based answer I linked above if you wanted to look into that.

LlamaSage
  • 73
  • 6