[英]Check if numeric input value exists in an array in JavaScript
我正在尝试检查输入的数字是否存在于数组中。 我已经尝试了很多东西,但没有任何工作。
function start() {
var arrNum = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
var input = document.getElementById('input').value;
var numberExist = document.getElementById('numberExist').innerHTML;
for (var i = 0; i < arrNum.length; i++) {
if (input === arrNum[i]) {
alert('input');
numberExist = ('The number ' + input + 'exists in the array');
} else {
numberExist = ('The number ' + input + 'does not exist in the array');
}
}
var arrNum = arrNum.join(' ');
document.getElementById('listOfvalues').innerHTML = ('The values in the array are ' + arrNum);
}
这里有一些提示:
input.value
转换为整数,例如使用一元+
运算符,因为HTMLInputElement#value
属性返回一个字符串。else
,除非你外面移动它的循环体块将第一个for循环迭代运行。 function start() { var arrNum = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20] var input = +document.getElementById('input').value var numberExist for (var i = 0; i < arrNum.length; i++) { if (input === arrNum[i]) { numberExist = 'The number ' + input + ' exists in the array' } } if (!numberExist) { numberExist = 'The number ' + input + ' does not exist in the array' } document.getElementById('listOfvalues').textContent = 'The values in the array are ' + arrNum.join(' ') document.getElementById('numberExist').textContent = numberExist } start()
<input type="number" id="input" value="18"> <div id="listOfvalues"></div> <div id="numberExist"></div>
当然,正如其他人指出的那样,使用Array#indexOf
方法可以大大简化此示例:
function start() { var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20] var input = +document.getElementById('input').value var numberExist = array.indexOf(input) < 0 ? 'The number ' + input + ' does not exist in the array' : 'The number ' + input + ' exists in the array' document.getElementById('listOfvalues').textContent = 'The values in the array are ' + array.join(' ') document.getElementById('numberExist').textContent = numberExist } start()
<input type="number" id="input" value="18"> <div id="listOfvalues"></div> <div id="numberExist"></div>
试试indexOf函数 例如:
arr = [1, 4, 18, 5, 10];
arr.indexOf(4); //returns 4
arr.indexOf(20); //returns -1
使用indexOf
方法
var arrNum = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function checkNumber(x){
if(arrNum.indexOf(x)!=-1){
alert('number exist')
}
else{
alert('dont exist')
}
}
checkNumber(8) // alert don't exist
试试这个:
function checkIfExists(arr, num){ for (var i = 0; i < arr.length; i +=1){ if(num === arr[i]){ return true; } } return false; } var arrNum = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; var userInput = document.getElementById('user-input'); arrNumStr = arrNum.join(","); document.getElementById('listOfvalues').innerHTML = ('The values in the array are ' + arrNumStr); document.getElementById('check').onclick = function () { var input = Number(userInput.value); // Using Number to convert from string to number //if (arrNum.indexOf(input) > -1) if (checkIfExists(arrNum,input)){ alert('Number is exists') }else{ alert('Number not exists'); } }
<input type="text" id="user-input"> <br> <input type="button" id="check" value="Check"> <br> <div id="listOfvalues" ></div>
将input
值转换为默认字符串的数字。
var input = +document.getElementById('input').value;
//OR input = Number(document.getElementById('input').value);
//OR input = parseInt(document.getElementById('input').value, 10);
如果找到数字,则中断循环,不要继续运行循环。
if (input === arrNum[i]) {
numberExist.innerHTML = ('The number ' + input + ' exists in the array');
break;
}
window.onload = start; function start() { var arrNum = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; var input = +document.getElementById('input').value; var numberExist = document.getElementById('numberExist'); for (var i = 0; i < arrNum.length; i++) { if (input === arrNum[i]) { numberExist.innerHTML = ('The number ' + input + ' exists in the array'); break; } else { numberExist.innerHTML = ('The number ' + input + ' does not exist in the array'); } } var arrNum = arrNum.join(' '); document.getElementById('listOfValues').innerHTML = ('The values in the array are ' + arrNum); }
<input type="number" id="input" value="18" onblur="start()"/> <button onclick="start()">Check</button> <div id="numberExist"></div> <div id="listOfValues"></div>
严格相等运算符比较类型:在 JavaScript 比较中应该使用哪个等于运算符 (== vs ===)? .
console.log("1 == 1 is", 1 == 1); console.log("1 == \\"1\\" is", 1 == "1"); console.log("1 === 1 is", 1 === 1); console.log("1 === \\"1\\" is", 1 === "1"); console.log("1 === parseInt(\\"1\\", 10) is", 1 === parseInt("1", 10));
尝试使用Number()
函数。 例如Number(input)
。 由于您输入的框类型不是Number
。 因此在input
变量中你总是得到字符串。 因此,您需要将字符串转换为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.