javascript - Angularjs Remove Objects With Empty Fields -
if have within scope variable so:
$scope.mylistold = [ { title: "first title", content: "first content" }, { title: "second title", content: "" }, { title: "third title", content: "" }, { title: "fourth title", content: "fourth content" } ];
how create new scope variable removed empty values on specific field? (in case content).
$scope.mylistnew = [ { title: "first title", content: "first content" }, { title: "fourth title", content: "fourth content" } ];
function removeifstringpropertyempty(arr, field) { return arr.filter(function(item) { return typeof item[field] === 'string' && item[field].length > 0; }); } var $scope = {"mylistold":[{"title":"first title","content":"first content"},{"title":"second title","content":""},{"title":"third title","content":""},{"title":"fourth title","content":"fourth content"}]}; $scope.mylistnew = removeifstringpropertyempty($scope.mylistold, 'content'); console.log($scope.mylistnew);
Comments
Post a Comment