简体   繁体   English

Javascript将数组中的项目添加到字符串

[英]Javascript Add Item in Array to string

I am trying to make a multiline string from taking an array of user ids and matching them to the username and then printing the string. 我正在尝试通过获取用户ID的数组并将它们与用户名匹配,然后打印该字符串来制作多行字符串。

This is what I have so far and am wondering what the proper way to do this would be: 这是到目前为止,我想知道执行此操作的正确方法是:

var names;
for(var i in array) {
    var obj = new NSOA.record.oaUser(i);
    var username = obj.name;
    names = names + username;
}

Ideally names would be a string that looks like: 理想情况下,名称应为类似于以下内容的字符串:

"Smith, Bob, Doe, Jane, Miller, Larry" “史密斯,鲍勃,母鹿,简,米勒,拉里”

Any help you can give would be very much appreciated! 您能提供的任何帮助将不胜感激!

If you're printing to HTML insert a <br> after each array element in the string: 如果要打印为HTML,则在字符串中的每个数组元素后插入<br>

var names = '';
for(var i in array) {
    var obj = new NSOA.record.oaUser(i);
    var username = obj.name;
    names = names + username + '<br>';
}

Push all the name in an array then finally Join it by any separator. 阵列中的所有名称,然后终于加入任何分隔它。

For example: 例如:

 var arr = []; arr.push('Smith'); arr.push('Bob'); arr.push('Doe'); arr.push('Jane'); arr.push('Miller'); arr.push('Larry'); document.write(arr.join(', ')); 

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

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