繁体   English   中英

如何从angularjs中的对象中删除空值

[英]How to remove empty value from object in angularjs

如何从angularjs-中的对象中删除空值

这是我的JavaScript代码

$scope.addRole = function() { 
                            var tempDept = "";
                            angular
                                    .forEach(
                                            $scope.departments,
                                            function(value, key) {
                                                if (value.name == currentDepartment) { console.log(value.name);
                                                    tempDept = currentDepartment;
                                                    if (value.roles != "") value.roles
                                                    //value.roles
                                                            .push({
                                                                name : $scope.role.name,
                                                                //responsibilities : $scope.role.responsibilities
                                                            });
                                                }
                                            });
                            $scope.save(tempDept);
                            $scope.role = {
                                name : "",
                                responsibilities : []
                            };

                                $scope.role.responsibilities.push({name : ""});
                                $scope.role.responsibilities.push({name : ""});
                                $scope.role.responsibilities.push({name : ""});
                                currentDepartment = "";
                                console.log($scope.role.responsibilities.name);
                                $scope.role.responsibilities.splice(index, 1);


                            //$scope.save();
                        }

我想删除对象的空值,怎么办? 给我一些建议。

您的意思是删除对象的属性吗?

喜欢 -

var a = {
    b : 1 
};

delete a.b; // a.b === undefined now 

您需要使用以下命令删除对象的属性:

delete object.property

在这种情况下,您只需将属性删除为:

delete $scope.role.responsibilities.name;

请参阅: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

您必须为此使用angular custom filter

我创建了一个plunker代码,这可能会对您有所帮助。 https://plnkr.co/edit/8YWxlr?p=预览

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM