简体   繁体   English

遍历对象数组

[英]Iterate through an array of Objects

I need to loop through an array of objects, and prompt the user to enter a student's name. 我需要遍历对象数组,并提示用户输入学生的姓名。 if the name exists I need to print the student's data to the screen or else keep on iterating until the user types quit. 如果名称存在,我需要将学生的数据打印到屏幕上,或者继续进行迭代,直到用户键入退出为止。

The issue I have is, if I type the name of student of the last object, the student's records will not print until the loop completes. 我遇到的问题是,如果键入最后一个对象的学生的姓名,则在循环完成之前,不会打印学生的记录。

Please see the code below: 请参见下面的代码:

 var students = [{ name: 'hacene', track: 'Full Stack JavaScript', achievements: 12, points: 1226 }, { name: 'dave', track: 'PHP Development', achievements: 128, points: 14868 }, { name: 'layla', track: 'iOS Development', achievements: 8, points: 901 }, { name: 'mika', track: 'Front-End Development', achievements: 15, points: 1666 }, { name: 'heisenberg', track: 'Blue Meth Manufacturing', achievements: 99, points: 9999 } ]; var student; var records = ''; var search; // Print function to print the content function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; } // Access each students records in the array / a loop is needed to achieve this for (var i = 0; i < students.length; i++) { student = students[i]; console.log(student); search = prompt('Please enter a student\\'s name to see his records: '); search = search.toLocaleLowerCase(); console.log(search); if (search === student.name) { records += '<h2><strong>Student: ' + student.name + '</strong><h2>'; records += '<p>Track: ' + student.track + '</p>'; records += '<p>Points: ' + student.points + '</p>'; records += '<p>Achievements: ' + student.achievements + '</p>'; break; } else if (search === 'quit') { break; } } // Print the students' name; track, achievments and points print(records); 

What I need is, to be able to print the records of a student, from the first prompt even if he's the last on the list. 我需要的是能够从第一个提示打印学生的记录,即使他是列表中的最后一个。

What you have done it's more like a guessing game : "find the name of the current student in the for loop" I don't know if you still need an answer but here you go : 您所做的更像是一个猜谜游戏:“在for循环中找到当前学生的姓名”,我不知道您是否仍然需要答案,但是您可以:

You only have to put the prompt before the for loop. 您只需要将提示放在for循环之前。 After the prompt you do the loop and stop if you find a result. 提示后,您进行循环,如果找到结果,则停止。

 var students = [{ name: 'hacene', track: 'Full Stack JavaScript', achievements: 12, points: 1226 }, { name: 'dave', track: 'PHP Development', achievements: 128, points: 14868 }, { name: 'layla', track: 'iOS Development', achievements: 8, points: 901 }, { name: 'mika', track: 'Front-End Development', achievements: 15, points: 1666 }, { name: 'heisenberg', track: 'Blue Meth Manufacturing', achievements: 99, points: 9999 } ]; var student; var records = ''; var search; // Print function to print the content function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; } search = prompt('Please enter a student\\'s name to see his records: '); // Access each students records in the array / a loop is needed to achieve this for (var i = 0; i < students.length; i++) { student = students[i]; search = search.toLocaleLowerCase(); if (search === student.name) { records += '<h2><strong>Student: ' + student.name + '</strong><h2>'; records += '<p>Track: ' + student.track + '</p>'; records += '<p>Points: ' + student.points + '</p>'; records += '<p>Achievements: ' + student.achievements + '</p>'; break; } } // Print the students' name; track, achievments and points print(records); 
 <div id="output"></div> 

You can add a if statement on your loop. 您可以在循环中添加if语句。

if( search != "quit") {
for (var i = 0; i < students.length; i++) {
  student = students[i];
  search = search.toLocaleLowerCase();

  if (search === student.name) {
    records += '<h2><strong>Student: ' + student.name + '</strong><h2>';
    records += '<p>Track: ' + student.track + '</p>';
    records += '<p>Points: ' + student.points + '</p>';
    records += '<p>Achievements: ' + student.achievements + '</p>';
    break;
  }
}
}

With it, if you type quit you will not enter in the loop. 使用它,如果您键入quit,则不会进入循环。

Even if it not fill your requirement the better way of doing it is using find method. 即使它不能满足您的要求,更好的方法还是使用find方法。

 var students = [{ name: 'hacene', track: 'Full Stack JavaScript', achievements: 12, points: 1226 }, { name: 'dave', track: 'PHP Development', achievements: 128, points: 14868 }, { name: 'layla', track: 'iOS Development', achievements: 8, points: 901 }, { name: 'mika', track: 'Front-End Development', achievements: 15, points: 1666 }, { name: 'heisenberg', track: 'Blue Meth Manufacturing', achievements: 99, points: 9999 } ]; var student; var records = ''; var search; // Print function to print the content function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; } function write(student) { records += '<h2><strong>Student: ' + student.name + '</strong><h2>'; records += '<p>Track: ' + student.track + '</p>'; records += '<p>Points: ' + student.points + '</p>'; records += '<p>Achievements: ' + student.achievements + '</p>'; print(records); } search = prompt('Please enter a student\\'s name to see his records: '); student = students.find((element) => { if (element.name == search) { return true; } }) if (student) { write(student); }; // Print the students' name; track, achievments and points 
 <div id="output"></div> 

I need to loop through an array of objects, and prompt the user to enter a student's name. 我需要遍历对象数组,并提示用户输入学生的姓名。 if the name exists I need to print the student's data to the screen or else keep on iterating until the user types quit. 如果名称存在,我需要将学生的数据打印到屏幕上,或者继续进行迭代,直到用户键入退出为止。

You need to rethink your algorithm: 您需要重新考虑算法:

get input from user
while user does not enter "quit"
    for each object in list
       if name is the same as input
           output the record
    get input from user

Note that you need to get the input from the user then iterate over the list to search for the correct object. 请注意,您需要从用户那里获取输入, 然后遍历列表以搜索正确的对象。

Alternatively, you could use an object instead of a list. 或者,您可以使用对象而不是列表。 This outer object would have the student's name as a key and the object record as a value. 该外部对象将以学生的姓名为键,而对象记录为值。 Now you can index on the student's name that the user inputs to get the record directly rather than searching through a list. 现在,您可以为用户输入的学生姓名建立索引,以直接获取记录,而无需搜索列表。

Instead of looping over students to prompt the user, you need to continue prompting the user until they enter quit or you have a match. 您无需继续提示学生以提示用户,而是需要继续提示用户,直到他们输入quit或您有一个匹配项为止。

Something like this: 像这样:

var quit = false
while(!quit) {
    search = prompt('Please enter a student\'s name to see his records: ');
    search = search.toLocaleLowerCase();

    if (search === 'quit') {
        quit = true;
        continue;
    }

    // search for student here and if match is found then set quit to true
}

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

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