[英]JavaScript - How can I use the print function to output 2 values that are stored in an array when those values are the same?
var students = [
{
name : "Dave",
track : "Front End Development",
achievement: 158,
points: 14703
},
{
name : "Jody",
track : "iOS Development with swift",
achievement: 133,
points: 16303
},
{
name : "Dave",
track : "PHP Development",
achievement: 55,
points: 2023
},
{
name : "Shawdesh",
track : "Learn Wordpress",
achievement: 40,
points: 1950
},
{
name : "Liton",
track : "Rails Development",
achievement: 98,
points: 450
}
]
var student;
var message = [];
var search;
function print(message) {
document.getElementById('output').innerHTML = message;
}
function getStudentReport(student) {
var report = "<h3> Student: " + student.name + "</h3>";
report += "<p>Track: "+ student.track + "</p>";
report += "<p>achievement: "+ student.achievement + "</p>";
report += "<p>Points: "+ student.points + "</p>";
return report;
}
while(true) {
search = prompt("Search the student recoards: type a name [Jody] or (type quit to exit.)");
if (search == null || search.toLowerCase() == 'quit') {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name.toLowerCase() === search.toLowerCase()) {
message.push(getStudentReport(student));
}
}
for (var j = 0; j < message.length; j += 1) {
document.write(message[j]);
}
}
When the prompt function runs, if I write #Dave
the message array stores 2 value because in #students
Array the Dave name uses two times. 当提示函数运行时,如果我编写#Dave
则消息数组存储2个值,因为在#students
Array中,Dave名称使用了两次。
But when I print this array by print function it only prints the last value. 但是,当我通过打印功能打印此数组时,它仅打印最后一个值。 But when I print it by document.write it prints 2 value. 但是当我通过document.write打印它时,它会打印2个值。
So, now my question is: How can I print 2 values that are stored in a message array using my #print
function? 所以,现在我的问题是:我怎样才能打印保存使用我的消息数组中的2个值#print
功能?
Sure it will add just the last message since you're using affection =
that will override the content, instea use +=
to append the new message to the old content, like : 可以肯定的是,由于您使用的情感=
将覆盖内容,因此只会添加最后一条消息,请使用+=
将新消息附加到旧内容上,例如:
function print(message) {
document.getElementById('output').innerHTML += message;
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.