Group results in autocompleted dropdown

Hi there :)!
I try to do a dropdown list in my app.
The thing is that I use sebdah/meteor-autocompletion package, because I want my results to be sorted in specific way and limited.

The last thing I need is to group my results.
For example: If I have 2 products named “blah” I want to get only 1 “blag” in my dropdown “autocompletion” list.

Some code:

HTML:

<template name="InvoicesEditInsertInsertForm">
   <input id="descriptionautocomplete" type="text" name="description" value="" class="form-control" autofocus="autofocus" placeholder="New Item...">
</template>

JS:

Template.InvoicesEditInsertInsertForm.rendered = function() {
AutoCompletion.init("input#descriptionautocomplete");
};

Template.InvoicesEditInsertInsertForm.events({

    'keyup input#descriptionautocomplete': function () {
            AutoCompletion.autocomplete({
              element: 'input#descriptionautocomplete',       // DOM identifier for the element
              collection: InvoicesItem,              // MeteorJS collection object
              field: 'description',                    // Document field name to search for
              limit: 5,                         // Max number of elements to show
              sort: { modifiedAt: -1 },
                });              // Sort object to filter results with
    
          },
    });

I need to use function that could group my “description” here.

I tried to do it in helper and I get it on my screen, but to be honest I don’t know how to put that into my dropdown :frowning:

try: function() {
        var item= InvoicesItem.find({},{sort:{modifiedAt:-1}}).fetch();
        var descriptions={};

        _.each(item,function(row){
            var description = row.description;

            if(descriptions[description]==null)
                descriptions[description]={description:description};
        });

        return _.values(descriptions);
  },

Thanks for all answers! :slight_smile:

Same as the SO answers…