如何更改字符串的输出?

时间:2015-08-28 21:42:18

标签: javascript arrays format

如何左对齐字符串的输出?我正在尝试打印第一封电子邮件,然后打印下一行等等

<html>
    <body>
        <p id="demo"></p>

        <script>

        //The email list
        var emailList = ["adam@yahoo.edu\n", "henry@yahoo.edu\n", "john@yahoo.edu\n", "sally@yahoo.edu\n", "adam@yahoo.edu\n", "david@yahoo.edu\n", "myhome@yahoo.edu\n", "david@yahoo.edu\n", "david@yahoo.edu\n", "hunger@yahoo.edu\n", "madison@yahoo.edu\n"];

        //remove the yahoo extension
        var emailUserHash = emailList.reduce(function(emailUsers, email, i) {
            var username = email.slice(0, email.indexOf('yahoo'));
            if(!emailUsers[username]) emailUsers[username] = true;
            return emailUsers;
        }, {});

        //call the emailUserHash function
        var emailUsers = Object.keys(emailUserHash)

        //sort the email list
        emailUsers.sort();

        //output the list
        document.write(emailUsers.join(', '));
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

您只需修改您的加入()

即可

使用'/ r / n'对您没有帮助

document.write(emailUsers.join('/r/n'));

所以你可以使用'br'这肯定会有效。

document.write(emailUsers.join('</br>'));