How to apply function against an accumulator and each key of object in JavaScript ?

In this article, we will see how to apply a function against an accumulator and each key in the object in JavaScript. Accumulators can be functions, objects, arrays, etc. In this article, we use the accumulator and key of an object against the function.
Approach: In our approach, we use to reduce. First, we get the keys from the object and make a function. After getting keys, we use reduce for apply function against a collection of keys and accumulator. In the reducing method, we use accumulator and key as arguments of the callback function which is our function.
Syntax:
Collection.reduce((accumulator, key)=>
function(accumulator,key), InitialValue);
Example 1:
Javascript
<script>Â Â Â Â function ApplyingFn() {Â
        // Function that apply against accumulator        function function(robj, key) {Â
            // Limits for salary for persons            var l1 = 'Below 30000';            var l2 = 'Below 40000';            var l3 = 'Below 50000';            var l4 = 'Above 50000';Â
            // Checking salary of persons            if (m[key] < 30000) {Â
                // Creating group for same                // salary below Limits                robj[l1].push(key);            }            if (m[key] < 40000) {Â
                // Creating group for same                // salary below Limits                robj[l2].push(key)            }            if (m[key] < 50000) {Â
                // Creating group for same                // salary below Limits                robj[l3].push(key)            }            else {Â
                // Creating group for same                // salary below Limits                robj[l4].push(key)            }            return robj;        }Â
        // object for the salary        var k = {            'Below 30000': [], 'Below 40000': [],            'Below 50000': [], 'Above 50000': []        };Â
        var m = {            'Rahul': 15000, 'Shyam': 220000,            'David': 420000, 'Sam': 35000, 'Ram': 450000        };Â
        // Apply Function against accumulator        // and key of object        var l = Object.keys(m).reduce((accumulator,             key) => function(accumulator, key), k);        console.log(l);    }    ApplyingFn();</script> |
Output:
{'Below 30000': ['Rahul'],
'Below 40000': ['Rahul', 'Sam'],
'Below 50000': ['Rahul', 'Sam'],
'Above 50000': ['Shyam', 'David','Ram']}
Example 2:
Javascript
<script>Â Â Â Â function ApplyingFn() {Â
        // Function which is applied         // against accumulator        function function(robj, value, key) {Â
            // Checking key is first or not            if (robj[value] == undefined) {Â
                // Creating group for same key                robj[value] = [];                robj[value].push(key);            }Â
            // If key is already taken            else {Â
                // Inserting value in group                robj[value].push(key)            }Â
            return robj;Â
        }        var m = {            'Ms.Dhoni': 'Cricket',             'Sunil Chhetri': 'Football',            'Rishabh Pant': 'Cricket',             'K.L Rahul': 'Cricket',            'Ishan Pandita': 'Football',        };Â
        // Apply Function against accumulator        // and key of object        var l = Object.keys(m).reduce((accumulator,            key) => function(accumulator, m[key], key), {});        console.log(l);    }    ApplyingFn();</script> |
Output:
{'Cricket': ['Ms.Dhoni', 'Rishabh Pant', 'K.L Rahul'],
'Football': ['Sunil Chhetri', 'Ishan Pandita']}
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



