0

Below is my custom directive and it will appear when our view will render.

  (function() {
    'use strict';

    function autoComplete(Configuration) {
            //link function will execute when this directive will render with view.
      function linkFn(scope, element) {
        //in scope.data I will get all my data from the controller but need to parse only unique data here
        //in scope.data I want only unique entries
        console.log(scope.data);
        var config = {
          source: function(request, response) {
            var results = $.ui.autocomplete.filter(scope.data || [], request.term);
            response(results.slice(0, Configuration.MAX_AUTOCOMPLETE_RESULT));
          }
        };
        //all set let's initialise the auto complete of jquery.
        element.autocomplete(config);
      }

      //our directive definition object.
      return {
        restrict: 'A',
        // Isolated scope for our directive.
        scope: {
          data: '='
        },
        link: linkFn
      };
    }

    var app = angular.module('app'),
      requires = [
        'Configuration',
        autoComplete
      ];
    app.directive('autoComplete', requires);
  }());

Here is directive I will get data from the controller and I want to parse unique data from scope.data.

3
  • You haven't provided any sample data. Commented Oct 26, 2016 at 6:20
  • Sample data will come dynamically . For reference point you can take example ["abc","xyz","abc","pqr"] @ sisyphus Commented Oct 26, 2016 at 7:12
  • I found the following to remove duplicates stackoverflow.com/questions/9229645/… Commented Oct 26, 2016 at 8:43

1 Answer 1

1

Not sure if this is what you are looking for but duplicates in a javascript array can be removed using Array.filter.

var arr =  ["abc","xyz","abc","pqr"]
console.log('duplicate array : ' + arr)

var unique_arr = arr.filter(function(element, index, array) {
    return index == array.indexOf(element);
})

console.log('unique arr ' + unique_arr);

For further reading, check here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your Answer.But I am looking into a filter which can remove all the duplicate from directive scope.data[].
Again, I don't understand. How is removing duplicates from scope.data[] within a directive different from any regular javascript code? Just replaca var arr to scope.data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.