0

I have this array:

$scope.damageEvants =
    [
       {"id":2, "StartDate":21/05/2012, EndDate:null},
       {"id":3, "StartDate":null, EndDate:02/09/2014},
       {"id":4, "StartDate":null, EndDate:null},
       {"id":5, "StartDate":21/05/2012, EndDate:null}
    ];

I want to filter(remove) all objects from array where property startDate and EndDate are null.

The result after filtering:

$scope.damageEvants =
    [
       {"id":2, "StartDate":21/05/2012, EndDate:null},
       {"id":3, "StartDate":null, EndDate:02/09/2014},
       {"id":5, "StartDate":21/05/2012, EndDate:null}
    ];

I tried this:

$scope.damageEvants.filter(return item.StartDate!== null && item.EndDate !== null  )

But it seem to be wrong way.

How can filter $scope.damageEvants using filter function?

6 Answers 6

3
$scope.damageEvants.filter(return item.StartDate!== null && item.EndDate !== null )

supposed to be

$scope.damageEvants.filter(function(item) {
     return item.StartDate!== null && item.EndDate !== null;
});

The filter

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

Comments

1
$scope.damageEvants = $scope.damageEvants.filter(function(item) {
     return item.StartDate!== null && item.EndDate !== null;
});

Comments

1

Just check if either date is set.

var array = [{ "id": 2, "StartDate": '21/05/2012', EndDate: null }, { "id": 3, "StartDate": null, EndDate: '02/09/2014' }, { "id": 4, "StartDate": null, EndDate: null }, { "id": 5, "StartDate": '21/05/2012', EndDate: null }],
    result = array.filter(function (a) {
        return a.StartDate || a.EndDate;
    });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

ES6 version

var array = [{ "id": 2, "StartDate": '21/05/2012', EndDate: null }, { "id": 3, "StartDate": null, EndDate: '02/09/2014' }, { "id": 4, "StartDate": null, EndDate: null }, { "id": 5, "StartDate": '21/05/2012', EndDate: null }],
    result = array.filter(a => a.StartDate || a.EndDate);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Comments

1

As Sushanth's answer indicates, .filter() expects to receive a function value, with that function returning a boolean value that determines if the current array member will be in the filtered array or not. Your code is passing filter a boolean value rather than a function value.

Comments

1

Another option would be to splice the object in an angular.forEach loop. This will actually remove the object from the original array rather than creating a new array.

Example: https://jsfiddle.net/gh03zur4/

Controller

        function Controller($scope) {
      $scope.damageEvants = [{
        "id": 2,
        "StartDate": 21 / 05 / 2012,
        EndDate: null
      }, {
        "id": 3,
        "StartDate": null,
        EndDate: 02 / 09 / 2014
      }, {
        "id": 4,
        "StartDate": null,
        EndDate: null
      }, {
        "id": 5,
        "StartDate": 21 / 05 / 2012,
        EndDate: null
      }];

      angular.forEach($scope.damageEvants, function(event) {
        if (!event.EndDate && !event.StartDate) {
          var idx = $scope.damageEvants.indexOf(event);
          $scope.damageEvants.splice(idx, 1);
        }
      });
    }

Another option based on Sushanth's answer would be to apply a filter in your ng-repeat list like this:

https://jsfiddle.net/qj7dz2eq/

Controller

$scope.filterEvents = function(item) {
  return item.StartDate !== null || item.EndDate !== null;
}

HTML

<ul>
    <li ng-repeat="item in damageEvants | filter:filterEvents">{{item.id}}</li>
</ul>

Doing it either one of these ways prevents the same array being bound to the $scope twice.

Comments

1

Can try like:

$scope.damageEvants = $scope.damageEvants.filter(function(obj) {
    return !(obj.StartDate === obj.EndDate && obj.StartDate === null);
});

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.