How to check the existence of key in an object using AngularJS ?

Given an object containing (key, value) pair and the task is to check whether a key exists in an object or not using AngularJS.

Approach: The approach is to use the in operator to check whether a key exists in object or not. In first example the key “Prop_1” is input and it exists in the object. In the second example the user can check which key they want to check for existence.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.obj1 =
                { "Prop_1": 1, "Prop_2": 2, "Prop_3": 3 };
            $scope.res = '';
            $scope.textval = "Prop_1";
            $scope.checkK = function () {
                var txtVal = $scope.textval;
                if (!(txtVal in $scope.obj1)) {
                    $scope.res = "Key not Exists.";
                } else {
                    $scope.res = "Key Exists";
                }
            }
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        Check if a key exists in 
        an object in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            Object - {{obj1}}<br><br>
            Enter the key: <input type="text" 
                    ng-model="textval">
            <br><br>
            <button ng-click="checkK()">
                Check here</button>
            <br><br>
            {{res}}<br>
        </div>
    </div>
</body>
  
</html

chevron_right


Output:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.obj1 =
                { "Prop_1": 1, "Prop_2": 2, "Prop_3": 3 };
            $scope.res = '';
            $scope.textval = "";
            $scope.checkK = function () {
                var txtVal = $scope.textval;
                if (!(txtVal in $scope.obj1)) {
                    $scope.res = "Key not Exists.";
                } else {
                    $scope.res = "Key Exists";
                }
            }
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        Check if a key exists in 
        an object in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            Object - {{obj1}}<br><br>
            Enter the key: <input type="text" 
                    ng-model="textval">
            <br><br>
            <button ng-click="checkK()">
                Check here</button>
            <br><br>
            {{res}}<br>
        </div>
    </div>
</body>
  
</html>   

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.