Open In App

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 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 Filter

<div ng-repeat="(key, value) in myObj"> 
...
</div>
{{ 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.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Angular first object in object
    </title>
    <script src=
    </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 2

Convert 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.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Angular first object in object
    </title>
    <script src=
    </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:


Article Tags :