javascript - Trouble removing duplicates from list -


//get email test area  var emaillist = document.getelementbyid("emailtextarea").value; var emaillistarray = emaillist.split("\n");  //remove yahoo , duplicates list  var usernamesarray = emaillistarray.map(function(val, index, arr) {     return val.slice(0, val.indexof('yahoo.com')); }); 

assuming:

emaillistarray = ['123@gmail.com','123@yahoo.com','123@gmail.com','123@hotmail.com'] 

you can this:

var usernamesarray = []; emaillistarray.foreach(function(item) {      if(usernamesarray.indexof(item) < 0 && item.indexof('yahoo.com') < 0) {          usernamesarray.push(item);      } }); 

first condition checks if element in turn not in array of results, second condition checks if element doesn't contain substring yahoo.com, if both true, element added results.

after that, usernamesarray should have:

[ '123@gmail.com', '123@hotmail.com' ] 

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