0

I have an array:

        var array = [
            {"name": "name1", "value": null},
            {"name": "name2", "value": 3},
            {"name": "name3", "value": false},
            {"name": "name4", "value": undefined},
            {"name": "name5", "value": "some text"}
        ];

And now I want to filter this array in my angular controller to get items with values -

{"name": "name2", "value": 3} and

{"name": "name5", "value": "some text"}

So, I tryed this way (and it doesn't work):

data = $filter('filter')(data, { value: emptyOrNull(value) });
function emptyOrNull(value) {
    return !(value === null || value === undefined || value === false);
}

What am I doing wrong?

1 Answer 1

2

a simple and effective way that can solve every problem it just tricky way but can help you out. it will handle everything null, undefined, (''),false`

just do this

<span ng-repeat="entry in ctrl.array | filter:!!entry.value ">{{entry.name}} </span>

working plunkr

script.js

(function(angular) {
  'use strict';
angular.module('FilterInControllerModule', []).
  controller('FilterController', ['filterFilter', function(filterFilter)  {

    this.array = [{
    "name": "name1",
    "value": null
}, {
    "name": "name2",
    "value": 3
}, {
    "name": "name3",
    "value": false
}, {
    "name": "name4",
    "value": undefined
}, {
    "name": "name5",
    "value": "some text"
}];

  }]);
})(window.angular);

can also use custom filter ..

.filter('customFilter',function(){
    return function(val,input){
      var result =[];
      angular.forEach(val,function(res){
        if(res.value && res.value.length || res.value){
           result.push(res);
        }

      });
      return result;

    }

  })

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example29-production</title>


  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
  <script src="script.js"></script>



</head>
<body ng-app="FilterInControllerModule">
  <div ng-controller="FilterController as ctrl">
  <div>
    All entries:
    <span ng-repeat="entry in ctrl.array | filter:!!entry.value ">{{entry.name}} </span>
  </div>
  <div>

  </div>
</div>
</body>
</html>

if you want to keep like your code you can simply do this.

data = $filter('filter')(data, {
    value: !!value
});
Sign up to request clarification or add additional context in comments.

6 Comments

In your plunkr result window: All entries: name3. It is not what I am expecting. Must be: All entries: name2 name5.
you mean value can be string , no problem keep one more param to check if it is string
matches your requirements .
Now it's matches my requirements, but I want filter array in controller (not in ng-repeat, but in javascript code).
why not in ng-repeat ? if you dont need in html then why to use filter just creation simple function
|

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.