1

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.

4

1 Answer 1

0

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

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

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

1 Comment

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. [].

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.