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

1

3 Answers 3

0

do you mean remove a property of an object ?

like -

var a = {
    b : 1 
};

delete a.b; // a.b === undefined now 
Sign up to request clarification or add additional context in comments.

5 Comments

i want to remove empty value of responsibilities
remove from the array? a specific one ? or all the ones that are null\undefined\empty?
all the ones that are null\undefined\empty
$scope.role.responsibilities.filter(function(res){ return res != null && res != undefiend && res != ""; });
Thanks i try this.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.