Javascript得到每个数组的总和

时间:2021-10-21 20:21:45

I've been trying to figure out how to perform this action properly and I'm sure I'm being an idiot (new to code here). I have a xml file (shown below), that I am parsing and attempting to build multiple tables for each data set based on the node "testset". The xml file will not change how it's designed, but will grow in how many "testsets" there are. I have the code below.

我一直在试图弄清楚如何正确地执行此操作,我确信我是一个白痴(这里是代码新手)。我有一个xml文件(如下所示),我正在解析并尝试基于节点“testset”为每个数据集构建多个表。 xml文件不会改变其设计方式,但会增加多少“测试集”。我有下面的代码。

I currently have testspassed and testpassed showing the 2 arrays as I need, but I can't figure out how to get the sum of each array separate (and expect that there will be a unknown amount of arrays in the further) and then I have to write it to the table. The code below currently adds together both parts of the array in tests passed, I need to keep them separated. So I want the array set to be 137, 70. I am thankful for any help or suggestions.

我目前有testspassed和testpasssed显示我需要的2个数组,但我无法弄清楚如何将每个数组的总和分开(并期望在更远的地方将有未知数量的数组)然后我有把它写到桌子上。下面的代码目前在传递的测试中将数组的两个部分加在一起,我需要将它们分开。所以我希望阵列设置为137,70。我感谢任何帮助或建议。

xml

<?xml version="1.0" encoding="utf-8"?>
<TestSets>
  <TestSet>
    <TestSetID>TestSet_iOS1</TestSetID>
    <Hardware>iPad Air 2</Hardware>
    <Version>iOS 8.3</Version>
    <Build>Pro 2.7.0.13 </Build>
    <Orientation>PORTRAIT</Orientation>
    <NumTestCases>46</NumTestCases>
    <NumTestScripts>17</NumTestScripts>
    <TotalTime>230891.363</TotalTime>
    <TestResults>
      <TestRun>
        <Date>24 Mar 2015 09:12:00</Date>
        <Hardware>iPad Air 2</Hardware>
        <Version>iOS 8.1.3</Version>
        <Build>2.7.0.607</Build>
        <Orientation>PORTRAIT</Orientation>
        <NumPassed>37</NumPassed>
        <NumFailed>31</NumFailed>
      </TestRun>
      <TestRun>
        <Date>27 Mar 2015 11:43:18</Date>
        <Hardware>iPad Air 2</Hardware>
        <Version>iOS 8.1.3</Version>
        <Build>Pro 2.7.0.615</Build>
        <Orientation>PORTRAIT</Orientation>
        <NumPassed>100</NumPassed>
        <NumFailed>25</NumFailed>
      </TestRun>
     </TestResults>
  </TestSet>
  <TestSet>
    <TestSetID>TestSet_iOS2</TestSetID>
    <Hardware>iPad Air 2</Hardware>
    <Version>iOS 8.1.3</Version>
    <Build>Pro 2.7.0.623</Build>
    <Orientation>LANDSCAPE</Orientation>
    <NumTestCases>38</NumTestCases>
    <NumTestScripts>9</NumTestScripts>
    <TotalTime>20800.255</TotalTime>
    <TestResults>
      <TestRun>
        <Date>30 Mar 2015 10:29:00</Date>
        <Hardware>iPad Air 2</Hardware>
        <Version>iOS 8.1.3</Version>
        <Build>2.7.0.615</Build>
        <Orientation>LANDSCAPE</Orientation>
        <NumPassed>34</NumPassed>
        <NumFailed>15</NumFailed>
      </TestRun>
      <TestRun>
        <Date>31 Mar 2015 20:00:02</Date>
        <Hardware>iPad Air 2</Hardware>
        <Version>iOS 8.1.3</Version>
        <Build>Pro 2.7.0.620</Build>
        <Orientation>LANDSCAPE</Orientation>
        <NumPassed>10</NumPassed>
        <NumFailed>19</NumFailed>
      </TestRun>
      <TestRun>
        <Date>02 Apr 2015 10:15:25</Date>
        <Hardware>iPad Air 2</Hardware>
        <Version>iOS 8.1.3</Version>
        <Build>Pro 2.7.0.623</Build>
        <Orientation>LANDSCAPE</Orientation>
        <NumPassed>26</NumPassed>
        <NumFailed>12</NumFailed>
      </TestRun>
    </TestResults>
  </TestSet>
</TestSets> 

Current Code

 document.write("<table><tr><th><st>Test Set ID</st></th><th>Hardware</th><th>Op Sys Version</th><th>App Build</th><th>Orientation</th><th>Number of Test Passed</th><th>Number of Test Failed</th></tr>");
var x = xml.getElementsByTagName("TestSet");
var sum = 0;
for (i = 0; i < x.length; i++) {
    document.write("<tr class='dynamictable'><td>");
    document.write(x[i].getElementsByTagName("TestSetID")[0].childNodes[0].nodeValue);
    document.write("</td><td>");
    document.write(x[i].getElementsByTagName("Hardware")[0].childNodes[0].nodeValue);
    document.write("</td><td>");
    document.write(x[i].getElementsByTagName("Version")[0].childNodes[0].nodeValue);
    document.write("</td><td>");
    document.write(x[i].getElementsByTagName("Build")[0].childNodes[0].nodeValue);
    document.write("</td><td>");
    document.write(x[i].getElementsByTagName("Orientation")[0].childNodes[0].nodeValue);
    var y = x[i].getElementsByTagName("TestRun");
    var testspassed = [];
    var testsfailed = [];
    for (j = 0; j < y.length; j++) {
        testspassed.push(y[j].getElementsByTagName("NumPassed")[0].childNodes[0].nodeValue);
        testsfailed.push(y[j].getElementsByTagName("NumFailed")[0].childNodes[0].nodeValue);
    }

    for (var k=0; k<testspassed.length; k++){
    sum+=testspassed[k]<<0;   
}
console.log(sum);        

}

1 个解决方案

#1


0  

Probably not the cleanest and best way but here's what I did to get it working:

可能不是最干净,最好的方式,但这就是我做的工作:

document.write("<table><tr><th><st>Test Set ID</st></th><th>Hardware</th><th>Op Sys Version</th><th>App Build</th><th>Orientation</th><th>Number of Test Passed</th><th>Number of Test Failed</th></tr>");
      var x = xml.getElementsByTagName("TestSet");
      for (i = 0; i < x.length; i++) {
        testspassed = [];
        testsfailed = [];
        passedsum = 0;
        failedsum = 0;
        var y = x[i].getElementsByTagName("TestRun");
        for (j = 0; j < y.length; j++) {
          passed = (y[j].getElementsByTagName("NumPassed")[0].childNodes[0].nodeValue);
          failed = (y[j].getElementsByTagName("NumFailed")[0].childNodes[0].nodeValue);
          passedsum += passed << 0;
          failedsum += failed << 0;
        }
        document.write("<tr class='dynamictable'><td>");
        document.write(x[i].getElementsByTagName("TestSetID")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Hardware")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Version")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Build")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Orientation")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(passedsum);
        document.write("</td><td>");
        document.write(failedsum);
        document.write("</td></tr>");
      }

#1


0  

Probably not the cleanest and best way but here's what I did to get it working:

可能不是最干净,最好的方式,但这就是我做的工作:

document.write("<table><tr><th><st>Test Set ID</st></th><th>Hardware</th><th>Op Sys Version</th><th>App Build</th><th>Orientation</th><th>Number of Test Passed</th><th>Number of Test Failed</th></tr>");
      var x = xml.getElementsByTagName("TestSet");
      for (i = 0; i < x.length; i++) {
        testspassed = [];
        testsfailed = [];
        passedsum = 0;
        failedsum = 0;
        var y = x[i].getElementsByTagName("TestRun");
        for (j = 0; j < y.length; j++) {
          passed = (y[j].getElementsByTagName("NumPassed")[0].childNodes[0].nodeValue);
          failed = (y[j].getElementsByTagName("NumFailed")[0].childNodes[0].nodeValue);
          passedsum += passed << 0;
          failedsum += failed << 0;
        }
        document.write("<tr class='dynamictable'><td>");
        document.write(x[i].getElementsByTagName("TestSetID")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Hardware")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Version")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Build")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Orientation")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(passedsum);
        document.write("</td><td>");
        document.write(failedsum);
        document.write("</td></tr>");
      }