简体   繁体   English

使用for循环和if..else语句迭代并检查数组

[英]Iterate and checking an array with for of loop and if..else statement

I am trying to make an array with names, then to iterate them using a for of loop and then to check the iteration using a if else statement. 我试图用名称创建一个数组,然后使用for循环对其进行迭代,然后使用if else语句检查迭代。 My code is below, but it did not work, I saw no result. 我的代码在下面,但是没有用,没有结果。 I tried with for in, the same bad result. 我尝试了in,同样的糟糕结果。 In Html file I have a paraghraph with demo id and a button with function for iterate and checking array 在HTML文件中,我有一个带有演示ID的paraghraph和一个具有迭代和检查数组功能的按钮

function availableName() {

 const check = document.getElementById('userID');
 const takenNames=['manuel','remus','manuel-remus','tomi','mirel'];
 const reservedNames=['cristina','angela','rusanda','costantin'];
 const givenNames=[...takenNames, ...reservedNames];

  for (givenName of givenNames) {
  if (givenName === check) {
      sayMessage=givenName +"Not available!";
 }
 else {
   sayMessage= givenName +"Available";
 }

 }
document.getElementById("available").innerHTML= sayMessage;
 };

Why not use the function isInArray? 为什么不使用isInArray函数?

function isInArray(value, array) {
    return array.indexOf(value) > -1;
}

function availableName() {
const check = document.getElementById('userID');
 const takenNames=['manuel','remus','manuel-remus','tomi','mirel'];
 const reservedNames=['cristina','angela','rusanda','costantin'];
 const givenNames=[...takenNames, ...reservedNames];
for (givenName of givenNames) {
  if (isInArray(check, givenNames)) {
      sayMessage=givenName +"Not available!";
 }
 else {
   sayMessage= givenName +"Available";
 }

 }
document.getElementById("available").innerHTML= sayMessage;
 };

First of all you need to get the value of the input using .value as stated in comments. 首先,您需要使用注释中所述的.value来获取input的值。

Then to check its existence in the array you can use one of Array 's built-in methods, such as includes() or indexOf() . 然后,要检查其是否存在于array ,可以使用Array的内置方法之一,例如includes()indexOf()

This is how should be your code: 这应该是您的代码:

function availableName() {
  const check = document.getElementById('userID').value;
  const takenNames = ['manuel', 'remus', 'manuel-remus', 'tomi', 'mirel'];
  const reservedNames = ['cristina', 'angela', 'rusanda', 'costantin'];
  const givenNames = [...takenNames, ...reservedNames];

  if (givenNames.indexOf(check) > 0) {
    sayMessage = check + " Not available!";
  } else {
    sayMessage = check + " Available";
  }
  document.getElementById("available").innerHTML = sayMessage;
};

Demo: 演示:

This is a Demo: 这是一个演示:

 function availableName() { //const check = document.getElementById('userID').value; const check = "cristina"; const takenNames = ['manuel', 'remus', 'manuel-remus', 'tomi', 'mirel']; const reservedNames = ['cristina', 'angela', 'rusanda', 'costantin']; const givenNames = [...takenNames, ...reservedNames]; if (givenNames.indexOf(check) > 0) { sayMessage = check + " Not available!"; } else { sayMessage = check + " Available"; } document.getElementById("available").innerHTML = sayMessage; }; availableName(); 
 <div id="available"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM