Javascript按键匹配数组值

时间:2019-07-10 15:41:19

标签: javascript

我正在读取一个HTML文件,该文件将控制台输出如下。

我正在尝试获取其值的特定键(Flesh-Kincaid等级水平,可读性得分,总单词数等)。

下面是我尝试过的代码:

$(document).ready(function() {
  document.getElementById('file').onchange = function() {

    const file = this.files[0];
    const reader = new FileReader();

    reader.onload = (event) => {
      const file = event.target.result;
      const allLines = file.split(/\r\n|\n/);
      //const tostring = JSON.stringify(allLines);
      //const trimhtml = tostring.toString().replace(/<\/?[^>]+(>|$)/g, "");
      let arr = trimhtml;

      console.log(typeof arr);    //string  

      arr.forEach((kv) => {       //Uncaught TypeError: arr.forEach is not a function
        if (kv.includes("Flesh-Kincaid Grade Level")) {
          let value = kv.split(':')[1];
          ans = parseFloat(value.replace(/ /g, ''));
        }               
      });

下面是我的整个HTML代码。

<!doctype html><html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>DITA QA Error Log</title><style type="text/css">
        body {font-family:sans-serif;}
        p {color:#25383C}
        ul {list-style: square}
        .tagerror {color: #B80000  }
        .metaerror {color: #B80000  }
        .tagwarning {color: #FFC125  }
        .tagwarningtrans {color: #FFC125  }
        .standard {color: #385E0F  }
        .syntaxerror {color: #B80000  }
        .prodterm {color: #330099  }
        .accerror {color: #330099  }
        .genterm {color: #330099  }
        #main {
        margin-left:10.2em;
        margin-right:10.2em;
        padding-left:1em;
        padding-right:1em;
        }
        ul.twocolumn {
        width: 400px;
        }
        ul.twocolumn li {
        width: 190px;
        float: left;
        }
        .info{
        color: green;
        }
        </style></head>
   <body>
      <h1>QA Error Log</h1>
      <table border="1" cellpadding="2" cellspacing="2">
         <tbody>
            <tr>
               <td rowspan="9">
                  <p align="center"><font size="20">PASS</font></p><img alt="Success" src="img/pass.png"><p align="center">Total violation count: 0</p>
               </td>
            </tr>
            <tr>
               <td><b>Pass/Fail</b></td>
               <td><b>Check Type</b></td>
               <td><b>Violations</b></td>
            </tr>
            <tr>
               <td><img alt="Success" src="img/passsm.png"></td>
               <td><font class="tagerror">Meta data errors</font></td>
               <td>0</td>
            </tr>
            <tr>
               <td><img alt="Success" src="img/passsm.png"></td>
               <td><font class="tagerror">Tagging errors</font></td>
               <td>0</td>
            </tr>
            <tr>
               <td><img alt="Success" src="img/passsm.png"></td>
               <td><font class="tagwarning">Tagging warnings</font></td>
               <td>0</td>
            </tr>
            <tr>
               <td><img alt="Success" src="img/passsm.png"></td>
               <td><font class="syntaxerror">Syntax errors</font></td>
               <td>0</td>
            </tr>
            <tr>
               <td><img alt="Success" src="img/passsm.png"></td>
               <td><font class="prodterm">Accessibility Errors</font></td>
               <td>0</td>
            </tr>
         </tbody>
      </table><br><hr>
      <h2>Summary of Content Processed</h2>
      <table border="1" cellpadding="2" cellspacing="2">
         <tbody>
            <tr>
               <td>Report run on</td>
               <td>2019-06-24+01:00</td>
            </tr>
            <tr>
               <td>Document information</td>
               <td>
                  <ul>
                     <p><b><u>PUBLICATION DETAILS</u></b></p>
                     <li>Publication Name: ESXi_6.7_GSG_Pub</li>
                     <li>Publication Title: VMware vSphere 6.7.x on Dell EMC PowerEdge Servers</li>
                     <li>Publication Alt Book Title: Getting Started Guide</li>
                     <li>Language Values Set: en-us</li>
                     <p><b><u>BOOKMAP DETAILS</u></b></p>
                     <li>Bookmap Name: ESXi_6.7_GSG_Map<br></li>
                     <li>Bookmap Title: VMware vSphere 6.7.x on Dell EMC PowerEdge Servers</li>
                     <li>Bookmap GUID: GUID-271EA1E3-91B0-4AE7-BAC8-01D579C9E36E</li>
                     <li>Version: 7<br></li>
                     <li>Revision: 2<br></li>
                     <li>Status: Released<br></li>
                     <p><b><u>DITA DETAILS</u></b></p>
                     <li>DITA versions used: 1.2</li>
                     <li>DITA Open Toolkit build log: <a href="GUID-271EA1E3-91B0-4AE7-BAC8-01D579C9E36E.ditamap_dellqa.log" target="_blank">open in new window</a><br></li>
                  </ul>
                  <p>Total Object Status:</p>Released : 100%<br><br></td>
            </tr>
            <tr>
               <td>Topic info</td>
               <td>
                  <ul>
                     <li>Total topics: 15</li>
                     <li>Concept: 1</li>
                     <li>Task: 5</li>
                     <li>Reference: 9</li>
                     <li>Glossary: 0</li>
                     <li>Total images: 0</li>
                  </ul>
               </td>
            </tr>
            <tr>
               <td>Readability Score

               </td>
               <td><pre>Flesh-Kincaid Grade Level:   12.87
Flesh Reading Ease Score:    35.88
Sentences:                   72
Words:                       1,339
Average Syllables per Word: 1.80
Average Words per Sentence:  18.60</pre></td>
            </tr>
            <tr>
               <td>Reuse Metrics Score</td>
               <td>50%
                  <!--1 div 2-->
               </td>
            </tr>
            <tr>
               <td>Element Counts</td>
               <td>
                  <p>Document contains 374 distinct tag values in 753 tags.</p>
                  <p>Total words: 2187</p>
               </td>
            </tr>
            <tr>
               <td>Non-Translated Elements</td>
               <td>
                  <p>-&lt;apiname&gt;</p>
                  <p>-&lt;cmdname&gt;</p>
                  <p>-&lt;codeblock&gt;</p>
                  <p>-&lt;codeph&gt;</p>
                  <p>-&lt;data&gt;</p>
                  <p>-&lt;draft-comment&gt;</p>
                  <p>-&lt;filepath&gt;</p>
                  <p>-&lt;msgblock&gt;</p>
                  <p>-&lt;msgph&gt;</p>
                  <p>In the bookmap:</p>
                  <p>-&lt;brand&gt;</p>
                  <p>-&lt;othermeta&gt;</p>
                  <p>-&lt;platform&gt;</p>
                  <p>-&lt;prodname&gt;</p>
                  <p>-&lt;bookpartno&gt;</p>
                  <p>-&lt;booknumber&gt;</p>
               </td>
            </tr>
         </tbody>
      </table><br><hr>
      <h2>QA Violations</h2>
      <p><b>Topic title: Notes, cautions,
            and warnings</b><br>
         GUID: GUID-5B8DE7B7-879F-45A4-88E0-732155904029<br>
         Object Title: AboutNotesCautionsWarnings<br>
         Version: 13<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 46</p>
      <hr>
      <p><b>Topic title: </b><br>
         GUID: GUID-089FFA4B-3A62-4B51-BDE1-309C58A451D9<br>
         Object Title: Copyright<br>
         Version: 34<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 34</p>
      <hr>
      <p><b>Topic title: Getting started
            with VMware vSphere</b><br>
         GUID: GUID-C18BA369-C295-40EA-B289-F82B4CD5270A<br>
         Object Title: Getting Started With VMware vSphere<br>
         Version: 7<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 81</p>
      <hr>
      <p><b>Topic title: Important information</b><br>
         GUID: GUID-688F17C9-D9A1-4ACD-B6FD-80B7F972A262<br>
         Object Title: Important information 14G<br>
         Version: 8<br>
         Revision: 3<br>
         Status: Released<br></p>
      <p>Word count: 151</p>
      <li style="color:#008000;">SUGGESTION: Word count should be less than 150</li>
      <hr>
      <p><b>Topic title: Download Dell EMC customized ESXi image</b><br>
         GUID: GUID-30CB4B16-9FD6-4F52-935F-429C0A898ED9<br>
         Object Title: Download the Dell customized ESXi image<br>
         Version: 14<br>
         Revision: 1<br>
         Status: Released<br></p>
      <p>Word count: 41</p>
      <hr>
      <p><b>Topic title: Download Dell EMC customized ESXi image from Dell EMC support site</b><br>
         GUID: GUID-205D5CBC-88AB-483D-994B-906A8AF5BBFF<br>
         Object Title: Download Dell EMC customized ESXi image from Dell EMC support site<br>
         Version: 1<br>
         Revision: 6<br>
         Status: Released<br></p>
      <p>Word count: 127</p>
      <hr>
      <p><b>Topic title: Download Dell EMC customized ESXi image from VMware site</b><br>
         GUID: GUID-D0AF0871-4EA2-476E-A945-D6BAA827DB25<br>
         Object Title: Download Dell EMC customized ESXi image from VMware site<br>
         Version: 1<br>
         Revision: 14<br>
         Status: Released<br></p>
      <p>Word count: 91</p>
      <hr>
      <p><b>Topic title: Configuring
            ESXi on Dell EMC PowerEdge server</b><br>
         GUID: GUID-7124A2CC-F94B-4B3E-BCF2-96CF63B8B8B7<br>
         Object Title: Configuring ESXi on the PowerEdge system<br>
         Version: 8<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 214</p>
      <li style="color:#800080;">ATTENTION: Word count should be less than 150</li>
      <hr>
      <p><b>Topic title: Getting help</b><br>
         GUID: GUID-E48F3CF0-33F6-4A4A-AF89-38CBD7ABCBC5<br>
         Object Title: Getting help<br>
         Version: 2<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 0</p>
      <hr>
      <p><b>Topic title: Contacting Dell EMC</b><br>
         GUID: GUID-06D4A080-0EF1-48E1-8789-35B73C773CEB<br>
         Object Title: Contacting Dell EMC<br>
         Version: 10<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 41</p>
      <hr>
      <p><b>Topic title: Related information
            for virtualization solutions</b><br>
         GUID: GUID-E2AF8030-350F-436F-BEA0-C18D4861F6EC<br>
         Object Title: Related information<br>
         Version: 28<br>
         Revision: 8<br>
         Status: Released<br></p>
      <p>Word count: 407</p>
      <li style="color:#0000FF;">CAUTION: Word count should be less than 150</li>
      <hr>
      <p><b>Topic title: Documentation
            resources</b><br>
         GUID: GUID-88115516-AE22-4529-9B71-6FED42AEF41D<br>
         Object Title: Documentation Resources<br>
         Version: 20<br>
         Revision: 5<br>
         Status: Released<br></p>
      <p>Word count: 503</p>
      <li style="color:#0000FF;">CAUTION: Word count should be less than 150</li>
      <hr>
      <p><b>Topic title: Download
            drivers and firmware</b><br>
         GUID: GUID-EC6E7830-9AA1-4151-92F9-02357D35BEF2<br>
         Object Title: Download drivers and firmware<br>
         Version: 2<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 66</p>
      <hr>
      <p><b>Topic title: Documentation feedback</b><br>
         GUID: GUID-88511184-CA07-479A-96AD-579B19148758<br>
         Object Title: Documentation feedback<br>
         Version: 1<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 25</p>
      <hr>
      <p><b>Topic title: Locating Service
            Tag of your system</b><br>
         GUID: GUID-D6FF1C05-42BF-4DFD-ACF8-7B5414F986B7<br>
         Object Title: Locating your system Service Tag-OS/Virtualization<br>
         Version: 2<br>
         Revision: 2<br>
         Status: Released<br></p>
      <p>Word count: 66</p>
      <hr>
      <h3>Validator Solutions</h3><a name="AccErr001"><strong>ACCESSIBILITY:</strong><i>AccErr001</i> - Table is missing&nbsp;<i>desc</i> element or it is empty.</a><br><p><strong>Solution: </strong>Place cursor after <i>title</i> element, insert&nbsp;<i>desc</i>&nbsp;element from the element list and add description.
      </p>
      <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr><a name="AccErr002"><strong>ACCESSIBILITY:</strong><i>AccErr002</i> - Table is missing <i>title</i> element.</a><br><p><strong>Solution: </strong>Place cursor inside <i>table</i> element, insert <i>title</i> element from the element list and add table title.
      </p>
      <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr><a name="AccErr003"><strong>ACCESSIBILITY:</strong><i>AccErr003</i> - Empty or missing <i>thead</i> element in the table.</a><br><p><strong>Solution: </strong>Place cursor inside <i>table</i> element, select <i>thead</i> element from the element list and add table heading.
      </p>
      <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr><a name="TagWar001"><strong>WARNING:</strong><i>TagWar001 </i> - <i>colspec</i> element is missing in table.</a><br><p><strong>Solution:</strong><ol type="a">
            <li>Insert <i>table</i> element.
            </li>
            <li>Place your cursor inside <i>tgroup</i> element and add <i>colspec</i> element from the element list. If you need three column table then you must add three <i>colspec</i> element.<br><p>Note: <i>colspec</i> element is not visible in <b>Tags on view.</b></p>
            </li>
            <li>Go to <b>Plain text view</b> and edit the attribute of each <i>colspec.</i></li>
            <li>Specify <i>colname</i> attribute as Col1, Col2 and so on.
            </li>
            <li>Specify the <i>width</i> of each column. To align the columns proportionately add ‘*’.
            </li>
         </ol>
         <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      </p>
      <hr><a name="TagWar002"><strong>WARNING:</strong><i>TagWar002 </i> - <i>colspec</i> element is missing in table.</a><br><p><strong>Solution:</strong><ol type="a">
            <li>Insert <i>table</i> element.
            </li>
            <li>Place your cursor inside <i>tgroup</i> element and add <i>colspec</i> element from the element list. If you need three column table then you must add three <i>colspec</i> element.<br><p>Note: <i>colspec</i> element is not visible in <b>Tags on view.</b></p>
            </li>
            <li>Go to <b>Plain text view</b> and edit the attribute of each <i>colspec.</i></li>
            <li>Specify <i>colname</i> attribute as Col1, Col2 and so on.
            </li>
            <li>Specify the <i>width</i> of each column. To align the columns proportionately add ‘*’.
            </li>
         </ol>
         <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      </p>
      <hr><a name="WebErr001"><strong>Error:</strong><i>WebErr001</i> - You must add complete URL in <i>href</i> attribute. Ex:&nbsp;https://www.dell.com/support/</a><br><p><strong>Solution: </strong> For latest updates, check this page: <a href="https://inside.dell.com/docs/DOC-326260" target="_blank">https://inside.dell.com/docs/DOC-326260</a></p>
      <ol type="a">
         <li>Select <b>Insert &gt; Link &gt; Web Link</b> from the XMetaL menu bar.
         </li>
         <li>Paste complete URL in the <i>Href</i> text box.<br><p>Note: Ensure to test if the URL is working before pasting in the Href text box and the URL must be Locale Neutral. </p>
         </li>
      </ol>
      <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr><a name="WebErr002"><strong>Error:</strong><i>WebErr002</i> - You must define attribute scope as ‘external’.</a><br><p><strong>Solution: </strong>For latest updates, check this page: <a href="https://inside.dell.com/docs/DOC-326260">https://inside.dell.com/docs/DOC-326260</a></p>
      <p>If you have added Web Link by selecting <b>Insert &gt; Link &gt; Web Link</b> from the XMetaL menu bar, the attribute scope = ‘external’ is automatically populated. Else, you must manually edit xref
         attribute.
      </p>
      <p><a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr><a name="WebErr003"><strong>Error:</strong><i>WebErr003</i> - You must define attribute format as ‘https’ only</a><br><p><strong>Solution: </strong>For latest updates, check this page: <a href="https://inside.dell.com/docs/DOC-326260">https://inside.dell.com/docs/DOC-326260</a></p>
      <p>If you have added Web Link by selecting <b>Insert &gt; Link &gt; Web Link</b> from the XMetaL menu bar and defined complete URL, the attribute format = ‘https’ is automatically populated. Else, you must
         manually edit xref attribute.<a href="javascript:history.back()">Click here to go back to issue.</a></p>
      <hr>
   </body>
</html>

问题是,我无法从上述输出中获取所需的密钥。

1 个答案:

答案 0 :(得分:0)

这就是我要做的:

  1. 扫描数组以查找包含键的项目。
  2. :分割字符串,并取第二项(即kv.split(':')[1])。
  3. value删除所有空格
  4. 将剥离的字符串解析为int或float。
  5. 将答案保存到变量中。

注意:

  1. 请注意,对于文本值,仅对于数字值,不需要阶段3和4。
  2. 您将需要对其中包含,的数字值(例如Words: 1,339)进行其他操作。
  3. 在我的代码中,我假设您在值之前有:

let arr = [

  "                     PUBLICATION DETAILS",
  "                     Publication Name: ESXi_6.7_GSG_Pub",
  "                     Publication Title: VMware vSphere 6.7.x on Dell EMC PowerEdge Servers",
  "                     Publication Alt Book Title: Getting Started Guide",
  "                     Language Values Set: en-us",
  "                     BOOKMAP DETAILS",
  "                     Bookmap Name: ESXi_6.7_GSG_Map",
  "                     Bookmap Title: VMware vSphere 6.7.x on Dell EMC PowerEdge Servers",
  "                     Bookmap GUID: GUID-271EA1E3-91B0-4AE7-BAC8-01D579C9E36E",
  "                     Version: 7",
  "                     Revision: 2",
  "                     Status: Released",
  "                     DITA DETAILS",
  "                     DITA versions used: 1.2",
  "                     DITA Open Toolkit build log: open in new window",
  "                  ",
  "                  Total Object Status:Released : 100%",
  "            ",
  "            ",
  "               Topic info",
  "               ",
  "                  ",
  "                     Total topics: 15",
  "                     Concept: 1",
  "                     Task: 5",
  "                     Reference: 9",
  "                     Glossary: 0",
  "                     Total images: 0",
  "                  ",
  "               ",
  "            ",
  "            ",
  "               Readability Score",
  "                  \t\t",
  "               ",
  "               Flesh-Kincaid Grade Level:   12.87",
  "Flesh Reading Ease Score:    35.88",
  "Sentences:                   72",
  "Words:                       1,339",
  "Average Syllables per Word: 1.80",
  "Average Words per Sentence:  18.60",
  "            ",
  "            ",
  "               Reuse Metrics Score",
  "               50%",
  "                  ",
  "               ",
  "            ",
  "            ",
  "               Element Counts",
  "               ",
  "                  Document contains 374 distinct tag values in 753 tags.",
  "                  Total words: 2187",
  "               ",
  "            ",
  "            "

];

arr.forEach((kv) => {
  if (kv.includes("Flesh-Kincaid Grade Level")) {
    let value = kv.split(':')[1];
    ans = parseFloat(value.replace(/ /g, ''));
  }

});

console.log(ans);