How to use foreach object keys and values in Angularjs?

How to use foreach object keys and values in Angularjs?

In this Post We Will Explain About is How to use foreach object keys and values in Angularjs? With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Iterating over arrays and objects in Angularjs Example

In this post we will show you Best way to implement Angular JS – angular.forEach – How to get key of the object?, hear for How to iterate easily over object properties in Angularjs with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :   AngularJS Login with PHP MySQLi




AngularJS forEach() Syntax

angular.forEach(object, iterator, [context])

object: The angularjs simple get object interate over.
iterator: The angularjs iterator methods or the code.
context: Object to become data context for the iterator methods.

Example : Angular forEach() with an Object

The Script

<script>
    var liveApp = angular.module('liveApp', []);

    liveApp.controller('liveCtrl', ['$scope', '$filter', function ($scope, $filter) {

        // 'students' OBJECT, WITH AN ARRAY OF DATA.
        $scope.students = {
            '05/22/2019': { 'item_name': 'krunal', 'preice': 25 },
            '06/30/2020': { 'item_name': 'Ankit', 'preice': 44 },
            '09/11/2020': { 'item_name': 'Chirag', 'preice': 32 },
            '01/15/2019': { 'item_name': 'Pradip', 'preice': 41 },
            '03/18/2021': { 'item_name': 'Hitesh', 'preice': 28 }
        }

        // ITERATE THROUGH ITEMS IN OBJECT AngularJS.
        angular.forEach($scope.students, function (value, key) {
            var mydt = new Date('01/05/2020');         // Get the DATE TO COMPARE (A SPECIFIED DATE).
            mydt = $filter('date')(mydt, 'shortDate');

            var lived1 = new Date();
            lived1 = $filter('date')(key, 'shortDate');


            if (Date.parse(lived1) >= Date.parse(mydt)) {
                console.log(key + ": " + value.item_name + ": " + value.preice);
            }
        });
    } ]
    );
</script>

Example 2 : Angular forEach() on a JSON Array

The Markup – index.html

<!DOCTYPE html>
<html>
<head>
<title>AngularJS forEach() Function – Simple Iterate or Loop Through Each Item in An Array or Object in AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
</head>
<body>
    <div ng-app="liveApp" 
        ng-controller="liveCtrl">

        <p><button ng-click="submit(items)">Display Items</button></p>
        <p>{{the_list}}</p>
    </div>
</body>

The Script (index.js)

<script>
    var liveApp = angular.module('liveApp', []);
    liveApp.controller('liveCtrl', ['$scope', function ($form) {

        $form.items = [
            { item_name: 'Laptop' },
            { item_name: 'Mobile' },
            { item_name: 'Mouse' },
            { item_name: 'Pendrive' },
            { item_name: 'Keyboards'}];

        $form.submit = function (items) {

            var liveAccess = [];

            angular.forEach(items, function (value, key) {
                liveAccess.push(value.item_name);
            });

            if (liveAccess.length > 0)
                $form.the_list = liveAccess.toString();
        }
    } ]
    );
</script>
</html>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   AngularJS UI Grid Example - step by step





Example

I hope you have Got What is How to iterate easily over object properties in Angularjs And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.