0

How to remove empty value from object in angularjs-

Here is my JavaScript code

$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..

  • Possible duplicate of [AngularJS remove empty value](http://stackoverflow.com/questions/32067327/angularjs-remove-empty-value) – Josip Ivic Mar 22 '16 at 12:27

3 Answers3

0

do you mean remove a property of an object ?

like -

var a = {
    b : 1 
};

delete a.b; // a.b === undefined now 
Ran Sasportas
  • 2,256
  • 1
  • 14
  • 21
0

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

Kailas
  • 7,350
  • 3
  • 47
  • 63
0

You have to use angular custom filter for it.

I have created a plunker code, which may help you. https://plnkr.co/edit/8YWxlr?p=preview

Ankit Pundhir
  • 1,097
  • 8
  • 13