Mjaaay
Mjaaay

Reputation: 15

JavaScript: else statement inside a for..in loop is breaking my if statement

I have an if statement inside a for in loop that works fine, but when I add an else statement at the end, the code breaks - as in the variable (key in this case) from the for..in loop doesn't get passed to the else statement. Here's the code:

config = {'test1':2, 'test2':2, 'test3':5, 'test4':8}

  for (key in config) {
    if (isNaN(item)) {
      return item;
    }
    if (key.indexOf(baseCcy) !== -1) {
      console.log("match config");
      item = parseFloat(item).toFixed(config[key]);
      return item;
    } else {
      item = parseFloat(item).toFixed(10);
      return item;
    }
  }

baseCcy and item is an input from angular, from the following code: | {{fill.price | decimalFilter:baseCcy}} The point of this is to create a custom filter and I'm doing a for..in loop inside the filter to achieve it. So far, it's working well, but the else statement just breaks it. The point of the else statement is if none of the input from item matches the config array, return the item with 10 decimals.

Of note, when I console.log key after the for..in loop, it only shows me "test1", but when I remove else statement (with only the two if), console.log key shows me "test1", "test2", "test3", "test4". '

Upvotes: 0

Views: 1474

Answers (2)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 19007

Just making some changes to your current logic, this must work for you.

config = {'test1':2, 'test2':2, 'test3':5, 'test4':8}
var newItemValue; // a new varialble
  for (key in config) {    
    if (isNaN(item)) {
      newItemValue = item
      break;  //break once you find the match
     //return item;      
    }
    else if (key.indexOf(baseCcy) !== -1) {
      console.log("match config");
      item = parseFloat(item).toFixed(config[key]);
      newItemValue = item
      break;//break once you find the match
      //return item;       
    } 
  }
  //if the loop was a failure, then do this by default.
  if(typeof newItemValue === 'undefined'){  // check if there is no value assigned to the new variable, if its not then the loop was a failure
      item = parseFloat(item).toFixed(10);
      newItemValue = item     
  }

Here is a link to Working JsFiddle

The output of the above logic is (when item = 12.12345678 and baseCcy ='test3')

12.12346

EDIT: After reading your last comment, I think this is what you want.

  config = {'test1':2, 'test2':2, 'test3':5, 'test4':8}

  for (key in config) {
    if (isNaN(item)) {
      return item;
    }
    if (key.indexOf(baseCcy) !== -1) {
      console.log("match config");
      item = parseFloat(item).toFixed(config[key]);
      return item;
    } 
  }
  //if the program has reached this line then the loop was a failure
  item = parseFloat(item).toFixed(10);
  return item;

Here no need of the new variables, another else and stuff.

Upvotes: 0

Damien Fayol
Damien Fayol

Reputation: 958

You can only return from a function !

If you want to exit a loop structure, use break.

Link to the relevant doc.

Example:

var conf;

for (key in config) {

    var item = config[key];

    if (isNaN(item)) {
        conf = item;
        break;
    }
    if (key.indexOf(baseCcy) !== -1) {
        console.log("match config");
        item = parseFloat(item).toFixed(config[key]);
        conf = item;
        break;
    } else {
        item = parseFloat(item).toFixed(10);
        conf = item;
        break;
    }
}

Upvotes: 2

Related Questions