简体   繁体   中英

Having trouble displaying an array in javascript

Hey guys I'm having some trouble with my code. I'm trying to loop through this array but whenever I do, my program show up blank in FireFox. Any help would be appreciated. thanks!

<!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>

With this program I'm trying to take the information that is in the nested array. search and add up the third numbers and the fourth numbers if the array names match.

This is probably what you are trying to achieve.

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);

我不认为您应该在数组中使用半冒号,我认为您应该使用昏迷。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM