How to check the existence of key in an object using AngularJS ? Last Updated : 04 Aug, 2022 Comments Improve Suggest changes 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 check the existence of key in an object using AngularJS ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies HTML 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 Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in 2 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 Check Whether an Object is a Date ? This article will show you how to check whether the given object is a Date or not. There are two methods to check for date objects, which are described below: Method 1: Using instanceof Operator The instanceof operator checks whether the prototype property of a constructor appears anywhere in the pr 2 min read How to Check if all Enum Values Exist in an Object in TypeScript ? To check if all values of an enum exist in an object in TypeScript, iterate through the enum values and verify their presence in the object. This ensures that the object encompasses all possible enum values, confirming completeness and adherence to the enum definition. There are various methods to c 3 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to 3 min read How Check if object value exists not add a new object to array using JavaScript ? In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { 3 min read Like