Skip to content

Contains

Checks if an object contains all the given keys

๐ŸŽฌ Usage

<script>
    import {contains} from "@sveu/shared/dicts"

    const obj = {a: 1, b: 2, c: 3}

    contains(obj, "a", "b") // true
</script>

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

๐Ÿ‘ป Arguments

Name Description Type Required
obj The object to check Record<string, any> yes
...keys The keys to check string[] yes

โ†ฉ๏ธ Returns

Boolean

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
/**
 * Checks if an object contains all the given keys
 *
 * @param obj - object to check
 *
 * @param keys - keys to check
 *
 * @returns boolean
 */
export function contains(obj: object, ...keys: string[]) {
    return keys.some((key) => key in obj)
}

Last update: 2023-02-14