[英]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.