Skip to content

Merge

Merge two lists and override the first list with the second list

๐ŸŽฌ Usage

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

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

    const other = [
        {id: 2, name: "Jane Doe"},
    ]

    const merged = merge(list, other, (item) => item.id) // [{id: 1, name: "John"}, {id: 2, name: "Jane Doe"}, {id: 3, name: "Jack"}]
</script>

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

๐Ÿ‘ป Arguments

Name Description Type Required
list The list to merge T[] Yes
other The list to merge with T[] Yes
fn To map items. (item: T) => K Yes

โ†ฉ๏ธ Returns

New list with the items from the other list merged in.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
/**
 * Merge two lists and override the first list with the second list
 *
 * @param list - The first list.
 *
 * @param other - The second list to merge.
 *
 * @param fn - A function that returns a unique key for each item in the array.
 *
 * @returns a new list with the second list merged into the first list.
 */
export function merge<T, K extends string | number | symbol>(
    list: T[],
    other: T[],
    fn: (item: T) => K
): T[] {
    if (!other && !list) return []

    if (!other) return list

    if (!list) return []

    if (!fn) return list

    return list.reduce((acc, r) => {
        const matched = other.find((o) => fn(r) === fn(o))
        return matched ? [...acc, matched] : [...acc, r]
    }, [] as T[])
}

Last update: 2023-02-14