2

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.

1
  • it worked when i make aId as integer ({aId: 5, aName: "Other"}) Commented Oct 15, 2015 at 7:18

3 Answers 3

1

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).

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

2 Comments

But i see in documentation that Primitive values are converted to strings. So if i do toString() its should work na. but it also does not.
Could you surround newId by " like s, {"newId":myObje.aId},
0

filter expects that the $scope.races must be an array, so your response from JSON must be added to an array and in this case your code will work

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

3 Comments

@ Aleksandar Gajic - yes its same structure. But it does not work
Here is the jsfiddle link jsfiddle.net/cajg4yxz/1 and you can see it is working in this example
because aId and newId are not of the same data type.
0

I make a solution for you and I think it's work fine. The problem is in your filter:

$scope.result = $filter('filter')($scope.races, {
    newId: +$scope.myObj.aId //parse $scope.myObj.aId to int
}, true);

Solution

Comments

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.