Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to apply function against an accumulator and each key of object...

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, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments