简体   繁体   English

JavaScript-当两个值相同时,如何使用打印功能输出存储在数组中的2个值?

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

相关问题 当我在javascript中打印这些cookie值时,cookie是否不存储? - Cookies are not Storing when i print those cookie values in javascript? 如何使用 Javascript 循环遍历数组并更改和添加额外的 HTML,以输出带有这些值的一些文本? - How Can I use Javascript to loop through an array and change and add extra HTML that outputs some text with those values? 如何使用javascript函数在键为奇数的数组中打印值 - How can I print values in array whose key is an odd number using javascript function 如何将Map函数用于具有值的数组和另一个数组作为Javascript中的值 - How can I use Map function for an Array with values and another array as a value in Javascript Javascript-如何使用十六进制值作为数组键? - Javascript - How can I use hexadecimal values as array keys? 如何从网站上的用户那里获取某些输入并将这些值作为特定参数传递到我的 javascript function 中? - How can I take certain inputs from a user on a website and pass those values as specific parameters in my javascript function? 如何在jQuery .html()函数中打印javascript数组值? - How to print a javascript array values in a jQuery .html() function? JavaScript:如何从函数返回两个值并在另一个函数中调用这两个变量? - JavaScript: How do I return two values from a function and call those two variables in another function? 如何将值传递到JavaScript数组? - How can i pass values to a javascript array? 如何在每个循环中保存变量的值,以便以后可以使用这些值 - How do I save the value of a variable on each loop so I can use those values later
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM