javascript - What is the JS pattern to get unique elements from nested array? -


i have following result mongodb.aggregate:

[{    _id: objectid(1),     _author: objectid(2),    comments: [       {          _author: objectid(2),          text: '...'       },       {          _author: objectid(3),          text: '...1'       },       {          _author: objectid(3),          text: '...2'       }...    ] }...] 

i need unique authors _author field elemnts (including nested):

var uniqauthors = magicfunction(result) // [objectid(2), objectid(3)] ; 

what best , compact way make pure js?

array.prototype.reduce can you:

var unique = result[0].comments.reduce(function(uniqueauthors, comment) {   if (uniqueauthors.indexof(comment._author) === -1) {      uniqueauthors.push(comment._author);   }   return uniqueauthors; }, []); //verify author document if (unique.indexof(result[0]._author) === -1) {    uniqueauthors.push(result[0]._author); } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -