无法在javascript中显示数组

时间:2015-07-22 14:54:07

标签: javascript arrays

嘿伙计我的代码遇到了一些问题。我正试图遍历这个数组,但每当我这样做时,我的程序在FireFox中显示为空白。任何帮助,将不胜感激。谢谢!

<!DOCTYPE html>
<html>
  <body>
    <p id="demo"></p>
    <script language="javascript" type="text/javascript">
      var finished1 = ["Firex";"Enrique";0;0];
      var finished2 = ["Detector";"Chris";0;0];
      var finished3 = ["Hpvv";"Diana";0;0];

      var info= new Array ( );
      info[0] = var info1 = ["Firex"; "Enrique"; 6; 20,000];
      info[1] = var info2 = ["Detector"; "Chris"; 7; 35,000];
      info[2] = var info3 = ["Hpvv"; "Diana"; 12; 10,000];
      info[3] = var info4 = ["Firex"; "Enrique"; 4; 25,000];
      info[4] = var info5 = ["Detector"; "Chris"; 3; 15,000];
      info[5] = var info6 = ["Hpvv"; "Diana"; 3; 30,000];
      info[6] = var info7 = ["Firex"; "Enrique"; 8; 10,000];
      info[7] = var info8 = ["Detector"; "Chris"; 5; 20,000];
      info[8] = var info9 = ["Hpvv"; "Diana"; 6; 15,000];
      info[9] = var info10 = ["Firex"; "Enrique"; 5; 25,000];
      info[10] = var info11 = ["Detector"; "Chris"; 6; 35,000];
      info[11] = var info12 = ["Hpvv"; "Diana"; 7; 35,000];
      info[12] = var info13 = ["Firex"; "Enrique"; 3 ; 40,000];
      info[13] = var info14 = ["Detector"; "Chris"; 9 ; 10,000];
      info[14] = var info15 = ["Hpvv"; "Diana"; 5 ; 15,000];

      for(i = 0; i < 15; i++) {
          if (info[0][0] = info[i][0]) {
              finished1[3] = info[0][2] + info[i][2];
              finished1[3] = info[0][3] + info[i][3];
          }

          if (info[1][0] = info[i][0]) {
              finished2[3] = info[1][2] + info[i][2];
              finished2[3] = info[1][2] + info[i][3];
          }

          if (info[2][0] = info[i][0]) {
              finished3[3] = info[2][2] + info[i][2];
              finished3[3] = info[2][3] + info[i][3];
          }
      }
    </script>
  </body>
</html>

使用这个程序,我正在尝试获取嵌套数组中的信息。如果数组名称匹配,则搜索并添加第三个数字和第四个数字。

2 个答案:

答案 0 :(得分:1)

这可能是你想要实现的目标。

var finished1 = ["Firex","Enrique",0,0];
var finished2 = ["Detector","Chris",0,0];
var finished3 = ["Hpvv","Diana",0,0];
var info = [];

info.push(["Firex", "Enrique", 6, 20000]);
info.push(["Detector", "Chris", 7, 35000]);
info.push(["Hpvv", "Diana", 12, 10000]);
info.push(["Firex", "Enrique", 4, 25000]);
info.push(["Detector", "Chris", 3, 15000]);
info.push(["Hpvv", "Diana", 3, 30000]);
info.push(["Firex", "Enrique", 8, 10000]);
info.push(["Detector", "Chris", 5, 20000]);
info.push(["Hpvv", "Diana", 6, 15000]);
info.push(["Firex", "Enrique", 5, 25000]);
info.push(["Detector", "Chris", 6, 35000]);
info.push(["Hpvv", "Diana", 7, 35000]);
info.push(["Firex", "Enrique", 3, 40000]);
info.push(["Detector", "Chris", 9, 10000]);
info.push(["Hpvv", "Diana", 5, 15000]);

info.forEach(function(element, index, array) {
  if(array[0][0] === array[index][0]) {
    finished1[3] = array[0][2] + array[index][2];
    finished1[3] = array[0][3] + array[index][3];
  }
  else if(info[1][0] === info[index][0]) {
    finished2[3] = array[1][2] + array[index][2];
    finished2[3] = array[1][2] + array[index][3];
  }
  else if(info[2][0] === info[index][0]) {
    finished3[3] = array[2][2] + array[index][2];
    finished3[3] = array[2][3] + array[index][3];
  }
});

console.log(finished1);
console.log(finished2);
console.log(finished3);

答案 1 :(得分:-1)

我认为你不应该在阵列中使用半结肠,我认为你应该使用昏迷。