Javascript Angularjs删除具有空字段的对象
如果我的范围内有这样一个变量:Javascript Angularjs删除具有空字段的对象,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,如果我的范围内有这样一个变量: $scope.myListOLD = [ { title: "First title", content: "First content" }, { title: "Second title", content: "" }, { title: "Third title", content: "" }, { title: "Fourth title", content: "Fourt
$scope.myListOLD =
[
{ title: "First title", content: "First content" },
{ title: "Second title", content: "" },
{ title: "Third title", content: "" },
{ title: "Fourth title", content: "Fourth content" }
];
如何创建一个新的范围变量来删除特定字段上的任何空值?(在本例中为内容)
使用
函数removeIfStringPropertyEmpty(arr,字段){
返回arr.filter(函数(项){
返回项[field]的类型=='string'&&item[field]。长度>0;
});
}
var$scope={“myListOLD”:[{“title”:“First title”,“content”:“First content”},{“title”:“Second title”,“content”:“},{“title”:“Third title”,“content”:“},{“title”:“Third title”,“content”:“Third content”},{“title”:“Third title”,“content”:“Third content”},};
$scope.myListNEW=removiefstringpropertyEmpty($scope.myListOLD,'content');
log($scope.myListNEW)代码>使用
函数removeIfStringPropertyEmpty(arr,字段){
返回arr.filter(函数(项){
返回项[field]的类型=='string'&&item[field]。长度>0;
});
}
var$scope={“myListOLD”:[{“title”:“First title”,“content”:“First content”},{“title”:“Second title”,“content”:“},{“title”:“Third title”,“content”:“},{“title”:“Third title”,“content”:“Third content”},{“title”:“Third title”,“content”:“Third content”},};
$scope.myListNEW=removiefstringpropertyEmpty($scope.myListOLD,'content');
log($scope.myListNEW)代码>
var-app=angular.module('app',[]);
应用程序控制器('homeCtrl',函数($scope){
$scope.myListOLD=
[
{标题:“第一个标题”,内容:“第一个内容”},
{标题:“第二个标题”,内容:},
{标题:“第三个标题”,内容:},
{标题:“第四标题”,内容:“第四内容”}
];
$scope.myListNEW=[];
angular.forEach($scope.myListOLD,函数(值,键){
如果(value.content!==“”)
$scope.myListNEW.push(值);
});
log($scope.myListNEW);
});
var-app=angular.module('app',[]);
应用程序控制器('homeCtrl',函数($scope){
$scope.myListOLD=
[
{标题:“第一个标题”,内容:“第一个内容”},
{标题:“第二个标题”,内容:},
{标题:“第三个标题”,内容:},
{标题:“第四标题”,内容:“第四内容”}
];
$scope.myListNEW=[];
angular.forEach($scope.myListOLD,函数(值,键){
如果(value.content!==“”)
$scope.myListNEW.push(值);
});
log($scope.myListNEW);
});
工作得很好!谢谢工作得很漂亮!谢谢
$scope.myListNEW =
[
{ title: "First title", content: "First content" },
{ title: "Fourth title", content: "Fourth content" }
];