Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to apply a filter to a JSON object without much luck.

The data structure is something like this:

$scope.jsonObj = {
    someid8979: {
        name: "Bill",
        age: 18
    },
    someid987: {
        name: "Ted",
        age: 17
    }
}

In my HTML, something like this:

<input ng-model="search">
<div ng-repeat="(key,val) in jsonObj | filter:search">
    {{key}} - {{val.name}} - {{val.age}}
</div>

In the past, I have always sent out the data as an array of objects, and indeed, if I did that here it would work, but due to the nature of the app, this isn't possible as I need to refer to properties on the object by name (the id) elsewhere in the code.

Is there any way to apply a filter to a JSON object similar to this?

I could create a separate array just for filtering purposes and target the JSON object for my other actions, but I'd rather not :)

Edit: I made a duplicate of the object as array and looped over that, which allows filtering. This solves my problem, but not in the way I had hoped. Could be good enough.

share|improve this question
1  
    
Yes, as I stated, it can be done with an array, I am just wondering if possible with an object. Thanks, though. –  Jason May 2 at 21:56
1  
github.com/angular/angular.js/blob/master/src/ng/filter/… So filter only works with arrays. –  dfsq May 2 at 22:16
    
@dfsq: wrong... –  Konstantin Krass May 2 at 22:17
    
@ Konstantin Krass Could you share how to do this? This doesn't work out of the box. Angular docs state clearly that filter accepts an array docs.angularjs.org/api/ng/filter/filter –  Jason May 2 at 22:27

1 Answer 1

You could place the comparator function in your controller like in this plunker:

http://plnkr.co/edit/n93RjIqLehVymmjjbTQd?p=preview

share|improve this answer
    
My model is not an array. The built-in $filter expects an array. I am just curious if there is a solution that can use an object. Not an array. {} vs. []. –  Jason May 2 at 22:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.