javascript - Add items to an array skipping duplicates -
i want add items array skipping duplicates. reason 1 item being added , second item not being added. here code.
var add = ['email1@gmail.com', 'email2@gmail.com', 'email1@gmail.com']; var main_messages = [] var function findmessages(messagelist) { return messagelist = from; } add.map(function(map){ = map if(main_messages.find(findmessages) === undefined){ main_messages.push(map) } }); console.log(main_messages)
so expected output should
['email1@gmail.com', 'email2@gmail.com']
but output i'm getting in code only
['email1@gmail.com']
what doing wrong , how can fix problem?
looks you're missing =
in return statement of findmessages
, you're setting from
messagelist
instead of comparing. here's fixed code
var add = ['email1@gmail.com', 'email2@gmail.com', 'email1@gmail.com']; var main_messages = [] var function findmessages(messagelist) { return messagelist === from; } add.map(function(map){ = map if(main_messages.find(findmessages) === undefined){ main_messages.push(map) } }); console.log(main_messages)
Comments
Post a Comment