0

was just wondering if you can filter an existing array with another array of filter.

So say our original json was:

[

   {
    "bookname": "harryporter",
    "year": "2000",
    "author":"J. K. Rowling"

   }, 

   {
    "bookname": "Sleepless",
    "year": "2003",
    "author":"Connie Ann Michael"

   },

   {
    "bookname": "No man's land",
    "year": "1993"
    "author":"Harold Pinter"

   }

];

Filter array:

[
"year":"1993"
"bookname":"No man"
]

Also is there a way to dynamically add elements to array when you edit input, perhaps type in text ?

for example:

Input

<div>
<input type="text" ng-model={{ filterArray.insert("bookname":"value") }}>Book Name
</div>

Filter

<div ng-repeat="books in originalJson | filter: filterArray">
{{books.author}}
</div>

I hope i have explained my question enough, cheers

6
  • you don't filter json. json is just a string. you decode that json string into a native JS array/object and filter that. Commented Aug 18, 2014 at 16:10
  • Not exactly an array filter or Angular, but maybe close to what you're looking for in underscore.js where. Commented Aug 18, 2014 at 16:10
  • Take a look at this filtering: stackoverflow.com/questions/11923142/… Get your filtering values from input, then create a JS function that compares those values with the JSON data. Set that JS function as the 'filter:' Commented Aug 18, 2014 at 16:12
  • Why are you using an filter array and not an object? Commented Aug 18, 2014 at 16:15
  • sorry guys, i have updated the question by adding code, for some reason, it didnt show up Commented Aug 18, 2014 at 16:18

1 Answer 1

1

If you want to filter by multiple values, you can pass in a filter-object.
Each key corresponds to a field-name and each value filters the items based on the corresponding key.

Book name: <input type="search" ng-model="filterObj.bookname" />
Year:      <input type="search" ng-model="filterObj.year" />
<div ng-repeat="book in books | filter:filterObj">...</div>

See, also, this short demo.


If you want to let the user dymanically specify the field to filter by, you can do that too (although it is not very common practice):

Filter by:
<input type="search" ng-model="dynamicField"
       placeholder="Field to filter by" />
<input type="search" ng-model="filterObj[dynamicField]"
       placeholder="Value to filter by" />
<div ng-repeat="book in books | filter:filterObj">...</div>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks alot, but what i want is to dynamically insert the name of each ng-model for each input into an array so that in my filter i reference those model's i am basically building a dynamic website where angualar isn't aware of any input until add them using the config.json file. cheers
@Pizzy213codes: I don't quite get what you want to achieve (or why). Can't the config.json initialize the filterObj as well ? Did you study my demo ?
you have literally saved my life !!!!!!!, i have been on this for a day now !!! O_o Thanks so much, please could you recommend a good brush up angular js tutorial. I can use angualr, but lack substantial knowledge of it:(
@Pizzy213codes: The official tutorial is one of the best around. Egghead.io has some "must-watch" short videos on various concepts as well. This tutorial was my way into Angular (but I think it's more on beginner level, which you are obviously not). Make sure you carefully read the docs (API reference) and from there you just code-code-code and delve into the source-code.
@Pizzy213codes: (You might also want to regularly check up on the changelog to see what has changed and what new features are available (or in beta).)

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.