javascript - Trying to remove duplicates from list not working -


i want print usernames of email addresses supplied in <textarea>, without duplicates.

<textarea id="emailtextarea" rows ="10" cols="25" > sara@yahoo.com adam@yahoo.com todd@yahoo.com henry@yahoo.com wright@yahoo.com sara@yahoo.com adam@yahoo.com todd@yahoo.com </textarea>  <body onload="emailsort()">  <script> //email sort function  function emailsort(){      //get data textarea      var emaillist = document.getelementbyid("emailtextarea").value;      //put array     var emaillistarray = emaillist.split("\n");      //remove extension , duplicates      var usernamesarray = emaillistarray.map(function(val, index, arr) {             var username = val.slice(0, val.indexof('yahoo.com'));         if(!val[username]) val[username] = true;         return emailusers;      });      //sort list      var sortedusernames = usernamesarray.sort();      //print out list      document.write(sortedusernames.join('</br>')); } 

in case can use lo-dash, try this:

var emails = _.compact(document.getelementbyid('emailtextarea').value.split("\n"));  console.log('sorted unique emails', _.uniq(emails).sort()); 

example: http://jsfiddle.net/j6cnzpdv/


without lodash:

var sorteduniqueemails = document.getelementbyid('emailtextarea').value     // build array of emails     .split("\n")      // sort     .sort()      // remove invalid/falsy entries (ie. blank lines)     .filter(function(line) {         return line.indexof('@') > -1; // assuming enough validate email     })      // remove duplicates     .reduce(function(result, email) {       if (result.indexof(email) < 0) result.push(email);        return result;     }, []);  console.log('sorted unique emails', sorteduniqueemails); 

example: http://jsfiddle.net/xa2ltxth/


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -