Skip to content

Group

Group a list of items by given key of item.

๐ŸŽฌ Usage

<script>
    import {group} from "@sveu/shared/lists"

    const actors = [
        {name: "Mila", age: 30},
        {name: "John", age: 40},
        {name: "Jane", age: 50},
        {name: "Emma", age: 30},
    ]

    const grouped = group(actors, actor => actor.age) // {30: [{name: "Mila", age: 30}, {name: "Emma", age: 30}], 40: [{name: "John", age: 40}], 50: [{name: "Jane", age: 50}]}
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ‘ป Arguments

Name Description Type Required
list The list to group T[] Yes
fn Function to get the key of each item in the list (item: T) => any Yes

โ†ฉ๏ธ Returns

A key-value pair of lists. The key is the key of the item, and the value is the list of items that have the same key.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
/**
 * Groups a list of items by a given function.
 *
 * @param list - List to group
 *
 * @param fn - Function to group by
 *
 * @returns A record of lists, where the key is the result of the function, and the value is the list of items that match the key
 */
export function group<T>(list: T[], fn: (item: T) => any) {
    return list.reduce((acc, item) => {
        const id = fn(item)
        const group_list = acc[id] ?? []
        return { ...acc, [id]: [...group_list, item] }
    }, {} as Record<string, T[]>)
}

Last update: 2023-03-07