简体   繁体   English

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

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

How to remove empty value from object in angularjs- 如何从angularjs-中的对象中删除空值

Here is my JavaScript code 这是我的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();
                        }

i want to remove empty value of object and how can do it? 我想删除对象的空值,怎么办? give me some suggestions.. 给我一些建议。

do you mean remove a property of an object ? 您的意思是删除对象的属性吗?

like - 喜欢 -

var a = {
    b : 1 
};

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

You need to delete the property of the object using the following: 您需要使用以下命令删除对象的属性:

delete object.property

Here in this case, you can simply delete the properties as: 在这种情况下,您只需将属性删除为:

delete $scope.role.responsibilities.name;

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

You have to use angular custom filter for it. 您必须为此使用angular custom filter

I have created a plunker code, which may help you. 我创建了一个plunker代码,这可能会对您有所帮助。 https://plnkr.co/edit/8YWxlr?p=preview https://plnkr.co/edit/8YWxlr?p=预览

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

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