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

14 Oct, 2020
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:
<!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> 
Output:
Example 2:
<!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>   
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
AngularJS-Misc
AngularJS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 10 Angular Libraries For Web Developers
Angular 10 (blur) Event
Angular PrimeNG Dropdown Component
How to make a Bootstrap Modal Popup in Angular 9/8 ?
How to create module with Routing in Angular 9 ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to set the default value for an HTML <select> element ?
How to update Node.js and NPM to next version ?
How to set input type date in dd-mm-yyyy format using HTML ?