0

I have a complex object (a user object) that has nested arrays and nested objects within it.

I have a search filter that is relatively complicated as well (checkboxes determining which items are returned, along with a search input).

Currently I search in an object like so:

for(var key in item){
  if(item[key] && item[key].length && String(item[key]).toLowerCase().indexOf($rootScope.filt.searchFilter.toLowerCase()) !== -1){
    realSave = true; 
  }
}

However, this only works for the first layer of objects within an item; I need to also search for objects within objects.

How can i do this? Is this a simpler way than the above? (Note, I can't just use ng-repeat="item in items | searchFilter" as this needs to also parse checkboxes and return values accordingly.

2 Answers 2

1

Try

realSave = (JSON.stringify(item).indexOf($rootScope.filt.searchFilter.toLowerCase()) !== -1)

(It's a long line, scroll to the right)

It will transform your whole object into a single string, then you can search for the sub-string you're looking for anywhere inside it.

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

3 Comments

Really? I tried searching how to convert an whole object to a string and got a whole lot of unhelpful answers. Thanks!
No worries! Please don't forget to validate my answer if it solved your issue.
Too much limitations in this approach like special symbols and searching in field names, not values.
0

You can use angular filter like this:

app.controller('MainCtrl', function($scope, $filter) {
  $scope.a = [{name : 'pit'}, {name : {a : 'pit'}}, {name : { a : { b : 'pit'}}}];
  $scope.find = $filter('filter')($scope.a, 'pit');
});

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

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.