How to check the existence of key in an object using AngularJS ? Last Updated : 04 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an object containing a (key, value) pair and the task is to check whether a key exists in an object or not using AngularJS. In order to check the existence of a key in an object, we will create an object having properties in the form of a key: value pair. Define a temporary variable that will hold the initial key of an object. Create a function expression that will be utilized to check whether the temporary variable that holds an initial key, exists or not in the given object, by comparing them. Approach: The approach is to use the in operator to check whether a key exists in an object or not. In the 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: In this example, the key "Prop_1" is input and checking whether it exists in the object or not. HTML <!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.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> <h3> Check if a key exists in an object in AngularJS </h3> <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}} </div> </div> </body> </html> Output: Example 2: In this example, the user will check which key they want to check for existence in the given object. HTML <!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.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> <h3> Check if a key exists in an object in AngularJS </h3> <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}} </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Filter an Array based on user input in AngularJS ? P PranchalKatiyar Follow Improve Article Tags : AngularJS AngularJS-Misc Similar Reads How to fetch the details using ng-repeat in AngularJS ? In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angul 2 min read How to render an Object in a Sorted order based upon Key in Angular ? An Object is a collection of properties, and a property is an association between a name (or key) and a value. A Property's value can be a function, in which case the property is known as a method. To achieve this, we can display the object's properties in a particular order, where the order is dete 3 min read How to filter by object property in AngularJS? Filtering by object property in AngularJS is the concept of choosing the specific objects from the data array which is based on the individual properties. In creating a web application, this is the most common task that deals with the data that needs to be sorted and then displayed to the user. Deve 6 min read How to select first object in object in AngularJS? The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from a 3 min read How to Filter an Array based on user input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular filters can be added in AngularJS to format data. 3 min read How to iterate over Object in Angular ? Objects consist of a set of key-value pairs, which are known as Properties. All Properties are named in JavaScript objects and the key part represents the Property name, while the value part represents the property Value. Each element(key-value pair) of the object can be utilized to perform a specif 3 min read Like