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". '
Aucun commentaire:
Enregistrer un commentaire