0

In AngularJS, you can filter an array of objects by an object property like this:

var filteredItems = $filter('filter')(items, {someObjProp: someValue});

My question is, how do i filter without hardcoding the 'someObjProp' - I want this to be a variable like:

var filteredItems = $filter('filter')(items, {arr[0].prop: someValue});

Thanks!

2 Answers 2

3

Setup the object in advance

var obj = {};
obj[arr[0].prop] = someValue;
Sign up to request clarification or add additional context in comments.

Comments

1

The second argument passed to the filter can be a function expression:

var filteredItems = $filter('filter')(items, function(value,index) {

   //you can do whatever you want in here with your 
   //object (value), just return true or false to fail or pass the filter

   return (value.arr[0].prop === someValue);
});

Simply return true for all objects that pass the filter and return false for objects that get filtered out.

Reference: https://docs.angularjs.org/api/ng/filter/filter

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.