Javascript 循环改变变量

Javascript 循环改变变量,javascript,for-loop,variables,Javascript,For Loop,Variables,我想为下面的代码找到一个更严肃的代码。我正在循环使用许多变量(player1、TSP1、player2、TSP2等等)。 这是一个丑陋的代码,它工作得很好: if (TSP1 < player1) { $("#errornotice").html("Minimum TSP player 1 " is " + player1); player1-list.focus(); } else if (TSP2 < player2) { $("#errornotice"

我想为下面的代码找到一个更严肃的代码。我正在循环使用许多变量(player1、TSP1、player2、TSP2等等)。 这是一个丑陋的代码,它工作得很好:

if (TSP1 < player1) {
    $("#errornotice").html("Minimum TSP player 1 " is " + player1);
    player1-list.focus();
} else if (TSP2 < player2) {
    $("#errornotice").html("Minimum TSP player 2 " is " + player2);
    player2-list.focus();
} else if (TSP3 < player3) {
    $("#errornotice").html("Minimum TSP player 3 " is " + player3);
    player3-list.focus();
} else if (TSP4 < player4) {
    $("#errornotice").html("Minimum TSP player 4 " is " + player4);
    player4-list.focus();
}

<b><span style="color:red" id ="errornotice"></span></b><br> 
if(TSP1
我怎样才能让它在一个循环中做同样的事情呢? 大概是这样的:

for (var a = 1; a < 4; a++){
     if (TSP+a < speler+a) {
          $("#errornotice").html("Minimum TSP player" +a+ " is " + player+a);
          Thuisspeler1-list+a.focus();
      }
}
for(var a=1;a<4;a++){
如果(TSP+a

很明显,syntaxis是完全错误的,但这只是给你一个我想要实现的想法。我真的被困在这里了,所以非常感谢您的帮助。

我建议您阅读此文章。

不确定是否理解代码片段的目的,如果您觉得这很明显,那么很抱歉,但是:

  • 你看过开关/机箱命令吗
  • 把这些变量设置成这样,你不能换个数组吗

Thx Joss。但我还是不能让它工作。你知道这里还有什么问题吗?我在原来的post.for(var a=1;a<4;a++)if(TSP[a]myObject['string'+int]。请注意,这是该页面上与您最相关的第二个解决方案:也许我问错了问题。让我试试另一种方法。如果我想一个接一个地提醒20个变量,它们的名称是variable1,variable2,。。。在js:Code:for(var a=1;a<20;a++){alert(variable[a])}for(var a=1;a<4;a++){if(thuisTSP+a