<html>
<body>
//我的文字区域
<textarea id="emailTextarea" rows ="30" cols="30" >
sara@yahoo.com
adam@yahoo.com
todd@yahoo.com
henry@yahoo.com
wright@yahoo.com
</textarea>
<p id="demo"></p>
<script>
//从textarea获取电子邮件列表的输入
//The email list
var emailList = document.getElementById("emailTextarea").value;
//删除yahoo扩展程序的功能
//remove the extension
var emailUserHash = emailList.reduce(function(emailUsers, email, i) {
var username = email.slice(0, email.indexOf('yahoo.com'));
if(!emailUsers[username]) emailUsers[username] = true;
return emailUsers;
}, {});
//调用emailUserHash函数
//call the emailUserHash function
var emailUsers = Object.keys(emailUserHash)
//对电子邮件列表进行排序
//sort the email list
emailUsers.sort();
//打印列表
//output the list
document.write(emailUsers.join('</br>'));
</script>
</body>
答案 0 :(得分:0)
认为这可能是你想要完成的事情:
var emailList = document.getElementById("emailTextarea").value;
var emailListArray = emailList.split("\n");
var usernamesArray = emailListArray.map(function(val, index, arr) {
return val.slice(0, val.indexOf('@yahoo.com'));
});
var sortedUsernames = usernamesArray.sort();
document.write(sortedUsernames.join('</br>'));
这将输出已排序并删除了@ yahoo.com的电子邮件用户名列表
答案 1 :(得分:0)
您应该将textarea解析为数组。
我用JQuery做了示例:https://jsfiddle.net/k8te23mf/
var arrayOfLines = $('#emailTextarea').val().split('@yahoo.com\n');
$('#demo').append(arrayOfLines+', ');