Search code examples
angularjsangular-filters

$filter not getting the desired output in angularjs


I have JSON field like below,

"newId":5

i have my search object like,

{aId: "5", aName: "Other"}

and have written filter like,

$filter('filter')($scope.races, {newId:myObj.aId},true);

I get an empty array as output. I tried adding toString() also did not work. Am i doing anything wrong.


Solution

  • You have two solution: 1- To make both fields of the same data type :

    $scope.races = [{"newId":5}]
    var myObje = {aId: 5, aName: "Other"};
    $filter('filter')($scope.races, {newId:myObje.aId},true);
    

    Note: aId = 5 (not "5").

    2- to write your custom comparator

    $filter('filter')($scope.races, {newId:myObje.aId},function(a,b){return a == b;});
    

    Note: a==b (not a === b).