0
<html>
<body>

//My text area

<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>

//Getting input from the textarea for the email list

    //The email list
    var emailList = document.getElementById("emailTextarea").value;

//function to remove the yahoo extension

    //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;
    }, {});

//calling the emailUserHash function

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

//Sort the email list

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

//Print out the list

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

MJB
  • 1
  • 1
  • Try to debug it with `alert()`; print out `emailUsers`, etc. – Mingle Li Aug 30 '15 at 20:38
  • Where is the actual question? It should be in the body of your post. Anyhow, *emailList* is a string, *reduce* is a method of arrays. Perhaps you mean to split the string on new lines to get an array, then you can use *reduce*. *Object.keys* on an array isn't going to help (here). Seems to me *replace* will do the job more efficiently. – RobG Aug 30 '15 at 20:54
  • `emailList.reduce` is not a function? – Mark Schultheiss Aug 30 '15 at 22:00

2 Answers2

0

Think this might be what you are trying to accomplish:

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>'));

This will output the list of email usernames sorted and with the @yahoo.com removed

John Gibbons
  • 322
  • 2
  • 13
0

You shall parse your textarea to array.

I made example with JQuery: https://jsfiddle.net/k8te23mf/

var arrayOfLines = $('#emailTextarea').val().split('@yahoo.com\n');
$('#demo').append(arrayOfLines+', ');
Shultc
  • 214
  • 1
  • 12