ng-options and unique filter not displaying angular.js

MitchVz picture MitchVz · Aug 22, 2013 · Viewed 19.9k times · Source

In my javascript I have an array

$scope.quoteList =        
    [
        {
            select: false,
            laymansDescription: "Nathan",
            quoteNumber: "ING-70440-21",
            version: "02",
            quoteDate: "Feb 5,2013",
            expirationDate: "Aug 5,2013",
            internalNotes: "This quote is using test data",
        },
        {
            select: false,
            laymansDescription: "Mitch",
            quoteNumber: "ING-70440-01",
            version: "02",
            quoteDate: "Feb 5,2013",
            expirationDate: "Aug 5,2013",
            internalNotes: "This quote is using test data",
        },
        {
            select: false,
            laymansDescription: "Stephen",
            quoteNumber: "ING-70440-01",
            version: "02",
            quoteDate: "Feb 5,2013",
            expirationDate: "Aug 5,2013",
            internalNotes: "This quote is using test data",
        }
    ];

And I am trying to make a selector that will only show unique quoteNumbers ie. ING-70440-21 and ING-70440-01. However when I try to use the 'unique' option in angular, nothing is showing up.

<select class="form-control" ng-options="quote.quoteNumber for quote in quoteList | unique:'quoteNumber'" ng-model="quoteModel1" />

It works fine without the unique tag. What am I doing wrong? I pretty new to angular so it might be something really simple.

Answer

AlwaysALearner picture AlwaysALearner · Aug 22, 2013

AngularJS does not have a built-in unique filter. You can do something like this to make one of your own:

app.filter('unique', function() {
    return function(input, key) {
        var unique = {};
        var uniqueList = [];
        for(var i = 0; i < input.length; i++){
            if(typeof unique[input[i][key]] == "undefined"){
                unique[input[i][key]] = "";
                uniqueList.push(input[i]);
            }
        }
        return uniqueList;
    };
});