2

Would like to do the following in the ng-repeat of a dropdown:

  • Fetch from JSON the categories of the variable product
  • Break down the categories array into new lines
  • Filter with unique
  • Display in dropdown

I have a demonstration at Plunker, and as you see, I've achieved the filter but I could not find a way to break the array and filter it.

My goal is to make a filter with the JSON requested and make a filter on the fly with the categories available.

1
  • In controller you need to map a new categories array by iterating products array Commented Jan 26, 2018 at 12:20

1 Answer 1

1

You don't need to inject 'angular.filter' to the controller

app.controller('MainCtrl', ['$scope', '$window', function($scope, $window) {

You actually do not need filter here, you can loop through the categories and add it to an array as follows

  $scope.filtered = [];
  angular.forEach($scope.products, function(key, value) {
    angular.forEach(key.categories, function(key, value) {
      if (!$scope.filtered.includes(key)) {
        $scope.filtered.push(key);
      }
    })
  })

DEMO

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

2 Comments

My mistake! I was Ctrl + Zing and forgot it inside. Now it is working as it should!
CHECK THE UPDATED ANSWER

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.