0

I am trying to create a new custom filter which will remove duplicates from the array of numbers , there's no error but no output as well , can someone please tell me what is wrong.

HTML CODE :

<!DOCTYPE>
<html>
    <head>
    </head>
    <body>
        <div ng-app="myApp">
            <div ng-controller="myCont">

                <div ng-repeat="num in uniqueArray | unique">
                    checking numbers :
                    {{num}}


                </div>

            </div>
        </div>
        <script src="jquery-3.0.0.js"></script>
        <script src="angular.js"></script>
        <script src="angular_try.js"></script>

    </body>
</html>

Angular js code :

var myApp = angular.module("myApp",[])

myApp.controller("myCont",["$scope","$filter",function($scope,$filter){

$scope.uniqueArray = [5,10,500,2,6,5,4,10,20,5]

}])

myApp.filter("unique",function(){
var arrNumb = [];
var arrNumb2 =[];
return function(input){
angular.forEach(input,function(value,index,obj){
if(arrNumb.indexOf(value)==-1)
{
    arrNumb.push(index);
    arrNumb2.push(value);
}

})
    }
return arrNumb2;
})

But there's nothing on the screen what am I doing wrong ???

Punit
  • 301
  • 2
  • 5
  • 14
  • Possible duplicate of [How to make ng-repeat filter out duplicate results](https://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results) – Jonathan Brizio Oct 31 '17 at 19:36
  • put in plunkr and share url here – rijin Oct 31 '17 at 19:36
  • You check for the presence of value in arrNumb, but then store indices inside. Why do you have two arrays? You need just one. And your arrays are defined and returned outside the function instead of inside. – JB Nizet Oct 31 '17 at 19:48
  • Thanks @JBNizet that was indeed the problem if you put it as an answer I would have accepted it – Punit Oct 31 '17 at 20:23

1 Answers1

0

You've probably just misplaced a return statement. Put return arrNumb2; inside the inner returned function (move it up one line) Also, as suggested in the comments, try moving the declarations inside as well.

var myApp = angular.module("myApp",[])

myApp.controller("myCont",["$scope","$filter",function($scope,$filter){

$scope.uniqueArray = [5,10,500,2,6,5,4,10,20,5]

}])

myApp.filter("unique",function(){

  return function(input){
    var arrNumb = [];
    var arrNumb2 =[];
    angular.forEach(input,function(value,index,obj){
    console.log("protima koo koo ")
    //arrNumb.push(value[name])
    if(arrNumb.indexOf(value)==-1)
    {
        arrNumb.push(index);
        arrNumb2.push(value);
    }

    })
    return arrNumb2;
  }

})
Vandesh
  • 6,368
  • 1
  • 26
  • 38
  • No it still doesn't work... In that case it throws error saying that Duplicates in array are not allowed – Punit Oct 31 '17 at 20:16