比较 javascript 中的字符串数组
Compare a String Array in javascript
我正在编写一个代码,其中有一个字符串要与变量数组进行比较。在这里,如果找到,我需要提醒找到匹配项。下面是我的代码。
在这里,我将单个字符串与字符串数组进行比较。我不是在比较两个不同的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> </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"> </td>
<td class="auto-style5" colspan="4"> </td>
<td colspan="4"> </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
时,它是一个字符串值。您正在将dataTerm
与variablesArray
中的元素进行比较,这些元素是数组(一旦您初始化然后使用 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);
}
}
}
相关文章:
- 将JSON转换为typescript中的字符串数组
- 在Javascript中将一个值和字符串数组转换为if语句
- 如何将正则表达式包含到字符串数组中
- JavaScript:将字符串数组转换为文本区域
- Knockout中的字符串数组
- 拆分字符串数组(JavaScript)后未定义
- 使用Javascript获取两个字符串之间的字符串数组
- 打印从Spring MVC Controller发送的字符串数组只显示“;toJSON”;在javascript中
- 将一个字符串数组解析为一个新的数组javascript
- AngularJS - 从字符串数组中连续更改文本
- 使用字符串数组中的字符填充 HTML 表
- 如何在 Javascript 中将字符串数组列表插入到表行中
- 创建二维字符串数组
- 如何从字符串数组中获取子字符串数组
- 如何将字符串数组转换为简单数组
- 按字母顺序、数字顺序和特殊字符对字符串数组进行排序
- 如何从字符串数组中获得8个随机唯一元素
- json_encode($myVar);正在给出地图,我想要一个字符串数组
- 函数,返回javascript中两个字符串数组的差值
- Javascript字符串数组转换为用逗号分隔的多个字符串