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
Post a Comment