比较 javascript 中的字符串数组

Compare a String Array in javascript

本文关键字:字符串 数组 javascript 比较      更新时间:2023-09-26

我正在编写一个代码,其中有一个字符串要与变量数组进行比较。在这里,如果找到,我需要提醒找到匹配项。下面是我的代码。

在这里,我将单个字符串与字符串数组进行比较。我不是在比较两个不同的js数组。

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function addTable() {
            var dataTerm = document.getElementById('Select2').value;
            var cancellations = new Array();
            var changeInfo = new Array();
            var idq = new Array();
            var others = new Array();
            var replace = new Array();
            var moreInfo = new Array();
            var salesRep = new Array();
            var custReq = new Array();
            var myTableDiv = document.getElementById("myDynamicTable");
            myTableDiv.border = "1";

            var variablesArray = new Array[cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq];
            for (var i = 0; i < variablesArray.length; i++) {
                if (dataTerm == variablesArray[i].value) {
                    alert("matched at" + variablesArray[i].value);
                }
            }
        }
    </script>
</head>
<body>
    <table class="auto-style1">
        <tr>
            <td class="auto-style3">Renewal/Product #</td>
            <td class="auto-style4">
                <input id="Text1" type="text" /></td>
            <td class="auto-style5">Product Title</td>
            <td>
                <input id="Text2" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Account #</td>
            <td class="auto-style4">
                <input id="Text3" type="text" /></td>
            <td class="auto-style5">Product Code</td>
            <td>
                <input id="Text4" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Invoice #</td>
            <td class="auto-style4">
                <input id="Text5" type="text" /></td>
            <td class="auto-style5">Suspended Date</td>
            <td>
                <input id="Text6" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Shipment / To #</td>
            <td class="auto-style4">
                <input id="Text7" type="text" /></td>
            <td class="auto-style5">Term Inc/ Dec</td>
            <td>
                <input id="Text8" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Tracking #</td>
            <td class="auto-style4">
                <input id="Text9" type="text" /></td>
            <td class="auto-style5">Qty Inc/ Dec From</td>
            <td>
                <input id="Text10" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Unit #</td>
            <td class="auto-style4">
                <input id="Text11" type="text" /></td>
            <td class="auto-style5">Qty Inc/ Dec To</td>
            <td>
                <input id="Text12" type="text" /></td>
        </tr>
        <tr>
            <td class="auto-style3">Billing Address</td>
            <td class="auto-style4">
                <input id="Text13" type="text" /></td>
            <td class="auto-style5">E-mail ID</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style3">Name</td>
            <td class="auto-style4">
                <input id="Text14" type="text" /></td>
            <td class="auto-style5">Request Type</td>
            <td>
                <select id="Select2" name="D2">
                    <option value="" disabled selected>Select your option</option>
                    <option value="Cancellations">Cancellations</option>
                    <option value="Changing Information">Changing Information</option>
                    <option value="Increase or Decrease Quantity/Term/Users">Increase or Decrease Quantity/Term/Users</option>
                    <option value="Others">Others</option>
                    <option value="Replacement">Replacement</option>
                    <option value="Fore more information">Fore more information</option>
                    <option value="Contacting Sales rep">Contacting Sales rep</option>
                    <option value="Requesting Customer">Requesting Customer</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="auto-style3" colspan="4">
                <input id="Button1" type="button" value="Click Me" onclick="addTable()" /></td>
            <td class="auto-style4" colspan="4">&nbsp;</td>
            <td class="auto-style5" colspan="4">&nbsp;</td>
            <td colspan="4">&nbsp;</td>
        </tr>
    </table>
    <br />
    <br />
    <br />
    <br />
    <div style="height: 420px" id="myDynamicTable">
    </div>
</body>
</html>

就我而言,令我惊讶的是,当我单击按钮时没有任何反应。

var cancellations = new Array();

在这里,您创建一个(新的)空数组。

var variablesArray = new Array(cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq);

在这里,您将创建一个包含多个空数组的数组。

if (dataTerm == variablesArray[i].value)

表达式 variablesArray[i] 的计算结果将计算为您放入此数组中的空数组之一。 表达式 .value 的计算结果将变为undefined,因为数组没有名为 value 的属性。 由于您拥有的字符串不等于未定义,因此条件永远不会为真。

看看:当你得到dataTerm时,它是一个字符串值。您正在将dataTermvariablesArray中的元素进行比较,这些元素是数组(一旦您初始化然后使用 new Array() .你不能比较如此不同的东西。

另一个主题:不要在

new Array[cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq];

Array函数,请使用括号。

new Array(cancellations, changeInfo, idq, others, replace, moreInfo, salesRep, custReq);

在循环中尝试这个:

for (var i = 0; i < variablesArray.length; i++) {
    for(j = 0; j < variablesArray[i].length; j++){
        if (dataTerm == variablesArray[i][j].value) {
            alert("matched at" + variablesArray[i][j].value);
        }
    }
}