JavaScript: for..in 循环中的 else 语句破坏了我的 if 语句
JavaScript: else statement inside a for..in loop is breaking my if statement
我在 for in
循环中有一个 if 语句,它工作正常,但是当我在末尾添加一个 else
语句时,代码中断 - 就像在变量中一样(在这种情况下是键) 来自 for..in
循环不会传递给 else
语句。这是代码:
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 和 item 是来自 angular 的输入,来自以下代码:| {{fill.price | decimalFilter:baseCcy}}
这样做的目的是创建一个自定义过滤器,我在里面做一个 for..in 循环过滤器来实现它。到目前为止,它运行良好,但 else 语句破坏了它。 else 语句的要点是如果 item
输入的 none 匹配配置数组,return 有 10 位小数的项目。
值得注意的是,当我在 for..in 循环之后 console.log key
时,它只显示 "test1",但是当我删除 else 语句时(只有两个 if ), console.log 键显示 "test1", "test2", "test3", "test4"。
'
您只能 return 来自函数 !
如果要退出循环结构,请使用break
。
Link相关doc.
示例:
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;
}
}
只需对您当前的逻辑进行一些更改,这一定对您有用。
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
}
这里是link到Working JsFiddle
以上逻辑的输出为(当item = 12.12345678
和baseCcy ='test3'
)
12.12346
编辑:看完你最后的评论后,我想这就是你想要的。
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;
这里不需要新的变量,还有其他的东西。
我在 for in
循环中有一个 if 语句,它工作正常,但是当我在末尾添加一个 else
语句时,代码中断 - 就像在变量中一样(在这种情况下是键) 来自 for..in
循环不会传递给 else
语句。这是代码:
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 和 item 是来自 angular 的输入,来自以下代码:| {{fill.price | decimalFilter:baseCcy}}
这样做的目的是创建一个自定义过滤器,我在里面做一个 for..in 循环过滤器来实现它。到目前为止,它运行良好,但 else 语句破坏了它。 else 语句的要点是如果 item
输入的 none 匹配配置数组,return 有 10 位小数的项目。
值得注意的是,当我在 for..in 循环之后 console.log key
时,它只显示 "test1",但是当我删除 else 语句时(只有两个 if ), console.log 键显示 "test1", "test2", "test3", "test4"。
'
您只能 return 来自函数 !
如果要退出循环结构,请使用break
。
Link相关doc.
示例:
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;
}
}
只需对您当前的逻辑进行一些更改,这一定对您有用。
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
}
这里是link到Working JsFiddle
以上逻辑的输出为(当item = 12.12345678
和baseCcy ='test3'
)
12.12346
编辑:看完你最后的评论后,我想这就是你想要的。
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;
这里不需要新的变量,还有其他的东西。