JavaScript - groupToMap() method

revision:


Category : array

This is an experimental technology

The groupToMap() method groups the elements of the calling array using the values returned by a provided testing function. The final returned Map uses the unique values from the test function as keys, which can be used to get the array of elements in each group.

The method is primarily useful when grouping elements that are associated with an object, and in particular when that object might change over time. If the object is invariant, you might instead represent it using a string, and group elements with Array.prototype.group().

Syntax :

        // Arrow function
        groupToMap((element) => { /* … */ } )
        groupToMap((element, index) => { /* … */ } )
        groupToMap((element, index, array) => { /* … */ } )

        // Callback function
        groupToMap(callbackFn)
        groupToMap(callbackFn, thisArg)

        // Inline callback function
        groupToMap(function(element) { /* … */ })
        groupToMap(function(element, index) { /* … */ })
        groupToMap(function(element, index, array){ /* … */ })
        groupToMap(function(element, index, array) { /* … */ }, thisArg)        
    

Parameters:

callbackFn : required. A function to execute for each element in the array. It should return a value that can get coerced into a property key (string or symbol) indicating the group of the current element. The function is called with the following arguments:

element : the current element being processed in the array.
index : the index of the current element being processed in the array.
array : the array group() was called upon.

thisArg : optional. A value to use as "this" when executing callbackFn.

Examples:

            const inventory = [
                { name: 'asparagus', type: 'vegetables', quantity: 9 },
                { name: 'bananas',  type: 'fruit', quantity: 5 },
                { name: 'goat', type: 'meat', quantity: 23 },
                { name: 'cherries', type: 'fruit', quantity: 12 },
                { name: 'fish', type: 'meat', quantity: 22 }
            ];
            const restock  = { restock: true };
            const sufficient = { restock: false };
            const result = inventory.groupToMap(({ quantity }) => quantity < 6 ? restock : sufficient);
            console.log(result.get(restock));
            // expected output: Array [Object { name: "bananas", type: "fruit", quantity: 5 }]

            // The key can be modified and still used
            restock['fast']  = true ;
            console.log(result.get(restock));
            // expected output: Array [Object { name: "bananas", type: "fruit", quantity: 5 }]

            // A new key can't be used, even if it has the same structure!
            const restock2  = { restock: true };
            console.log(result.get(restock2));
            // expected output: undefined
        

Practical examples

example:
code:
                    
                

example:
code:
                    
                

example:
code: