4 Example(s) of angular isArray


Description :

angular.isArray is used to check whether the object is an array or not? and the return value is Boolean true otherwise return false. See the code snippet:


angular isArray Example - 1
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isArrayController">
        isArray: {{isArray}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isArrayController', ['$scope', function ($scope) {
        var values = [{ name: 'Jimmi', gender: 'male' }, { name: 'Marry', gender: 'female' }, { name: 'Bob', gender: 'male' }];
        $scope.isArray = angular.isArray(values)
    }]);
    
</script>

Output

Description :

In this example of angular.isArray, it will return false because we are testing with an object not with array


angular isArray Example - 2
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isArrayController">
        isArray: {{isArray}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isArrayController', ['$scope', function ($scope) {
        var values = { name: 'Jimi', gender: 'male' };
        $scope.isArray = angular.isArray(values)
    }]);
 
</script>

Output

Description :

In this example it will check if angular.isArray returns true then push obj's value in 'nameArr' array and show the name in result window.


angular isArray Example - 3
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isArrayController">
        isArray: {{isArray}}
        <div ng-repeat="nm in nameArr">{{nm.name}}</div>
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isArrayController', ['$scope', function ($scope) {
        $scope.nameArr = [];
        var values = [{ name: 'Jimmi', gender: 'male' }, { name: 'Marry', gender: 'female' }, { name: 'Bob', gender: 'male' }];
        if (angular.isArray(values)) {
            $scope.isArray = true;
            angular.forEach(values, function (value, key) {
                $scope.nameArr.push(value)
            })
        }
    }]);
  
 </script>

Output

Description :

In this example, there are two variable one contains string value and another is an array and it checks both of them and display the output in result window. See the code snippet:


angular isArray Example - 4
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isArrayController as vm">
        isArray1: {{vm.isArray1}}<br />
        isArray2: {{vm.isArray2}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isArrayController', [function () {
        var vm = this;
        var arrs = [1, 2, 3, 4, 5, 6];
        var str = "www.learnkode.com";
        vm.isArray1 = angular.isArray(arrs);
        vm.isArray2 = angular.isArray(str);
    }]);
 
</script>

Output