Reputation: 13616
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?
Upvotes: 0
Views: 74
Reputation: 55740
$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
Upvotes: 3
Reputation: 16805
Can try like:
$scope.damageEvants = $scope.damageEvants.filter(function(obj) {
return !(obj.StartDate === obj.EndDate && obj.StartDate === null);
});
Upvotes: 1
Reputation: 1860
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.
Upvotes: 1
Reputation: 816
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.
Upvotes: 1
Reputation: 386520
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>');
Upvotes: 1
Reputation: 962
$scope.damageEvants = $scope.damageEvants.filter(function(item) {
return item.StartDate!== null && item.EndDate !== null;
});
Upvotes: 1