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" }          ]; 

use array.prototype.filter

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

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -