如果在javascript中的数组中找不到输入的值,如何获取警报消息

时间:2017-06-15 04:20:33

标签: javascript arrays

我有一个对象数组,我可以使用for循环遍历数组,如果找到值,我可以将它打印到页面而没有问题,但我正在尝试获取一条警告消息显示是否找不到该值并继续询问下一个值,直到用户键入quit。我的代码的问题是警报消息一直出现,直到循环结束,如果找不到值。这是我的代码:

var message = '';
var student;
var search;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getStudentReport( student ) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

function findStudent( look ){

    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (look === student.name) {
          message += getStudentReport( student );
          print(message);
        } else{
            alert(look + ' was not found');
        }

    }
    print(message);
}


while (true){
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
    if (search === null || search === 'quit'){
        break;
    }
  findStudent(search); 

}

感谢任何帮助。感谢。

5 个答案:

答案 0 :(得分:1)

让我们解释这部分代码:

function findStudent( look ) {

    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (look === student.name) {
          message += getStudentReport( student );
          print(message);
        } else{
            alert(look + ' was not found');
        }

    }
    print(message);
}

它的作用是在数组/对象上循环使用for循环,并为数组内的每条记录打印一条消息或警报。那就是问题所在。您只想检查是否找到了值,如果是,则打印或发出警报。

function findStudent( look ) {

    var found = false;

    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (look === student.name) {
            found = true;
            break;  // if the student is found no need to loop further
                    // move on with rest of code
        }
    }

    // so if the student was found in the for loop, found will be true
    // else found will be false cause we set it to false at the beginning
    if(found) {
        message += getStudentReport( student );
        print(message);
    } else {
        alert(look + ' was not found');
    }

}

答案 1 :(得分:1)

我认为这对你来说可能很有帮助https://codepen.io/kalaiselvan/pen/YQGbar

    var message = '';
    var student;
    var search;
    function print(message) {
      var outputDiv = document.getElementById('output');
      outputDiv.innerHTML = message;
    }

    function getStudentReport( student ) {
      var report = '<h2>Student: ' + student.name + '</h2>';
      report += '<p>Track: ' + student.track + '</p>';
      report += '<p>Points: ' + student.points + '</p>';
      report += '<p>Achievements: ' + student.achievements + '</p>';
      return report;
    }

    function findStudent( look ){
    var flag=0;
        for (var i = 0; i < students.length; i += 1) {
            student = students[i];      
            if (look === student.name) {
              message += getStudentReport( student );        
              print(message);  
              flag=0;
              break;
            } else{          
              flag=1;
            }
        }
      if(flag==1){
        alert(look + ' was not found');
        showprompt();
      }   
    }

    function showprompt(){
      search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
      if (search != null && search !== "quit"){    
          findStudent(search); 
      }
    }
    showprompt();

答案 2 :(得分:0)

你可以使用indexof https://www.w3schools.com/jsref/jsref_indexof_array.asp

else if (students.indexOf(look) == -1){
    alert(look + ' was not found');
})

答案 3 :(得分:0)

您需要在findStudent()函数内停止循环。
首先,添加一个布尔变量:var flag = true;
其次,将它用作while循环的条件:while(flag){...}
最后,每当想要停止循环时分配flag = false

function findStudent(look){

for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (look === student.name) {
      message += getStudentReport( student );
      print(message);
      //possible stop here if you want
      flag = false;
    } else{
        alert(look + ' was not found');
      //possible stop here if you want
      flag = false;
    }

}
print(message);
//possible stop here if you want
flag = false;
}

答案 4 :(得分:0)

var message = '';
var student;
var search;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getStudentReport( student ) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

function findStudent( look ){
    message = '';

    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (look === student.name) {
            message += getStudentReport(student);
        }
    }
    return message;
}


while (true){
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
    if (search === null || search === 'quit'){
        break;
    }

    if(findStudent(search)){
        print(findPurpose(search));
        break;
    } else {
        alert(search + ' was not found');
    }

}