Skip to content

Select

Selects a list of values from a list of objects

๐ŸŽฌ Usage

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

    const list = [
        {id: 1, name: "John"},
        {id: 2, name: "Jane"},
        {id: 3, name: "Jack"},
    ]

    const names = select(list, (item) => item.name) // ["John", "Jane", "Jack"]

    const ids = select(list, (item) => item.name, (item) => item.id > 1) // [2, 3]
</script>

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

๐Ÿ‘ป Arguments

Name Description Type Required
list The list to select from T[] Yes
fn To map items. (item: T) => K Yes
condition To filter items. (item: T) => boolean No

โ†ฉ๏ธ Returns

A list of values of the item.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
/**
 * Selects a list of values from a list of objects
 *
 * @param list - The list of objects to select from
 *
 * @param fn - The function to select the value from the object
 *
 * @param condition - The condition to filter the list by
 *
 * @returns The list of values
 */
export function select<T, K>(
    list: T[],
    fn: (item: T) => K,
    condition?: (item: T) => boolean
) {
    return list.reduce((acc, item) => {
        if (condition && !condition(item)) return acc
        return [...acc, fn(item)]
    }, [] as K[])
}

Last update: 2023-02-14