使用for循环遍历多个变量?

时间:2019-01-30 17:37:02

标签: javascript

晚上好,我想知道是否有一种方法可以通过for循环遍历多个变量? 我设置了许多变量,这是我的代码:

if (MedicalIndex11 == 1) {
  if (MedicalIndex11 != MedicalIndex1) {
  temp = Medical11.value;
  Medical11.value = Medical1.value;
  Medical1.value = temp;
} else if (MedicalIndex11 != MedicalIndex3) {
  temp = Medical11.value;
  Medical11.value = Medical3.value;
  Medical3.value= temp;
} else if (MedicalIndex11 != MedicalIndex5) {
  temp = Medical11.value;
  Medical11.value = Medical5.value;
  Medical5.value = temp;
} else if (MedicalIndex11 != MedicalIndex7) {
  temp = Medical11.value;
  Medical11.value = Medical7.value;
  Medical7.value = temp;
} else if (MedicalIndex11 != MedicalIndex9) { 
  temp = Medical11.value;
  Medical11.value = Medical9.value;
  Medical9.value = temp;
} else {
}

编辑:这是带有for循环和数组的代码的工作示例:

for (i = 10; i < 30; i++) {
  if (IndexArray[i] == 1) {
    if (IndexArray[i] != MedicalIndex1) {
      temp = MedFieldArray[i].value;
      MedFieldArray[i].value = Medical1.value;
      Medical1.value = temp;
  } else if (IndexArray[i] != MedicalIndex3) { 
      temp = MedFieldArray[i].value;
      MedFieldArray[i].value = Medical3.value;
      Medical3.value= temp;
  } else if (IndexArray[i] != MedicalIndex5) {
      temp = MedFieldArray[i].value;
      MedFieldArray[i].value = Medical5.value;
      Medical5.value = temp;
  } else if (IndexArray[i] != MedicalIndex7) {
      temp = MedFieldArray[i].value;
      MedFieldArray[i].value = Medical7.value;
      Medical7.value = temp;
  } else if (IndexArray[i] != MedicalIndex9) { 
      temp = MedFieldArray[i].value;
      MedFieldArray[i].value = Medical9.value;
      Medical9.value = temp;
  } else {
 }
}
}

我运行了代码,语法似乎不错,但是它不想交换变量的值。我从PDF文档的用户输入中获取了 Medical 变量 编辑2:通过在PDF表单上的用户输入找到医学变量,这是代码:

var Medical1 = this.getField("Medications 1");
var Medical3 = this.getField("Medications 3");
var Medical5 = this.getField("Medications 5");
var Medical7 = this.getField("Medications 7");
var Medical9 = this.getField("Medications 9");
var Medical11 = this.getField("Medications 11");
var Medical13 = this.getField("Medications 13");
var Medical15 = this.getField("Medications 15");
var Medical17 = this.getField("Medications 17");
var Medical19 = this.getField("Medications 19");
var Medical21 = this.getField("Medications 21");
var Medical23 = this.getField("Medications 23");
var Medical25 = this.getField("Medications 25");
var Medical27 = this.getField("Medications 27");
var Medical29 = this.getField("Medications 29");
var Medical31 = this.getField("Medications 31");
var Medical33 = this.getField("Medications 33");
var Medical35 = this.getField("Medications 35");
var Medical37 = this.getField("Medications 37");
var Medical39 = this.getField("Medications 39");
var MedicalIndex1 = 0
var MedicalIndex3 = null
var MedicalIndex5 = null
var MedicalIndex7 = null
var MedicalIndex9 = null
var MedicalIndex11 = null
var MedicalIndex13 = null
var MedicalIndex15 = null
var MedicalIndex17 = null
var MedicalIndex19 = null
var MedicalIndex21 = null
var MedicalIndex23 = null
var MedicalIndex25 = null
var MedicalIndex27 = null
var MedicalIndex29 = null
var MedicalIndex31 = null
var MedicalIndex33 = null
var MedicalIndex35 = null
var MedicalIndex37 = null
var MedicalIndex39 = null
var MedicalArray = ["DEPRESSION", "ANGINA", "BIPOLAR DEPRESSION", 
"ATHEROSCLEROSIS", "HEART ATTACK"];
 var temp = 0

目标是交换具有MedicalArray所列诊断结果的变量,以便前5个 Medical 变量包含Medical Array中几个字符串之一。

1 个答案:

答案 0 :(得分:0)

您如何看待这样的解决方案?

if (MedicalIndex11 == 1) 
{
  var i;
  for(i=1;i<=9;i+=2)
    swap(11,i);
}
else
{
}

function swap(a, b)
{
  var temp;
  if (window['MedicalIndex' + a] == window['MedicalIndex' + b]) return;
  temp = window['Medical' + a].value;
  window['Medical' + a].value = window['Medical' + b].value;
  window['Medical' + b].value = temp;
}