How to select first object in object in AngularJS? Last Updated : 18 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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 any source is an object containing objects(like JSON files). Altering the source file is very inconvenient so we need to be able to do something in the application itself. The most effective way is hidden in the problem itself. If objects are not iterable, then convert them to iterable. The following are 2 approaches that can be utilized to select the first object in object in AngularJS, which is described below: Approach 1: Using the ng-repeat Directive and the limitTo FilterThe ng-repeat is able to iterate over the properties of objects which in our case are objects themselves. The following syntax is used:<div ng-repeat="(key, value) in myObj"> ... </div>limitTo filter creates a new array or string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array, string, or number, as specified by the value and sign (positive or negative) of the limit. The following syntax will be used to bind the HTML:{{ limitTo_expression | limitTo : limit : begin}}Example 1: In this example, the value of the limit can be changed to get the element(object in this case) of the iterable. HTML <!DOCTYPE html> <html> <head> <title> Angular first object in object </title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> </head> <body ng-app='MyApp' ng-controller='MyCtrl'> <div ng-repeat="(key, obj) in institute | limitTo : 1"> <ul> <li ng-repeat="(prop, value) in obj"> {{prop}}: {{value}} </li> </ul> </div> <script> var app = angular.module('MyApp', []); app.controller('MyCtrl', function($scope) { $scope.institute = { 'school': { location: 'Jamshedpur', name: 'RV' }, 'college': { location: 'Kolkata', name: 'Jadavpur' } }; var instilist = new Array(); for (key of Object.keys($scope.institute)) { instilist.push($scope.institute[key]); } $scope.institute = instilist; }); </script> </body> </html> Output: Approach 2Convert the object of objects into an array of objects and use the indexing method of square brackets([]) to display the first object of the array. Syntax: {{name_of_the array[index]}}Example 2: In this example, the value of the index is 0 for the first object of the array. Since the elements of the array are objects we can refer to the properties of the objects using the (.) operator. HTML <!DOCTYPE html> <html> <head> <title> Angular first object in object </title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <div ng-app="MyApp" ng-controller="MyCtrl"> <p> The Name of the school is: {{institute[0].name}} </p> </div> <script> var app = angular.module('MyApp', []); app.controller('MyCtrl', function($scope) { $scope.institute = { school: { location: 'Jamshedpur', name: 'RV' }, college: { location: 'Kolkata', name: 'Jadavpur' } }; var instilist = new Array(); for (key of Object.keys($scope.institute)) { instilist.push($scope.institute[key]); } $scope.institute = instilist; }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to select first object in object in AngularJS? A anushil8991 Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads 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 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 How to bind select element to an object in Angular ? 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. An Angular service is a broad category that consists of an 4 min read How to select an element by its class name in AngularJS? Given an HTML document and the task is to select an element by its className using AngularJS. The elements can be selected with the help of a class name using the document.querySelector() method that is used to return the first element that matches a specified CSS selector(s) in the document. Approa 2 min read How to iterate over filtered (ng-repeat filter) collection of objects in AngularJS ? The task is to iterate over a collection of objects already filtered by ng-repeat filters and after clicking a button, change some property of only those objects which got filtered. Suppose you have a collection of objects that were filtered by a text search within a title property on each object. N 3 min read How to Filter Multiple Values in AngularJS ? AngularJS is one of the popular frameworks of many web developers to create dynamic single-page web applications. To make the application more and more dynamic, we can use the filtering of data feature to dynamically show the data to the user as per the input or selection. These provide a better use 6 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 Sort List by Date Filter in AngularJS ? AngularJS is a feature-based JavaScript framework that uses various tools to create dynamic single-page web applications. While developing the application we have the requirement to play with the data in AngularJS and sort this list of data by a date properly in descending order. This can be done us 5 min read 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 apply filters to *ngFor in Angular ? In this article, we will see How to apply filters to *ngFor in AngularJS, along with understanding their basic implementation through the examples. NgFor is used as a Structural Directive that renders each element for the given collection each element can be displayed on the page. Implementing the f 3 min read Like