mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-30 18:23:22 +03:00
1.4 KiB
1.4 KiB
includes
::: info
This function is only available in es-toolkit/compat
for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet.
When imported from es-toolkit/compat
, it behaves exactly like lodash and provides the same functionalities, as detailed here.
:::
Checks if a specified value exists within a given source, which can be an array, an object, or a string.
The comparison uses SameValueZero to check for inclusion.
Signature
function includes<T>(arr: T[], item: T, fromIndex?: number): boolean;
function includes<T extends Record<string, any>>(obj: T, value: T[keyof T], fromIndex?: number): boolean;
function includes(str: string, substr: string, fromIndex?: number): boolean;
Parameters
source
(T[] | Record<string, any> | string
): The source to search in. It can be an array, an object, or a string.target
(T
): The value to search for in the source.fromIndex
(number
): The index to start searching from. If negative, it is treated as an offset from the end of the source.
Returns
(boolean
): true
if the value is found in the source, false
otherwise.
Examples
includes([1, 2, 3], 2); // true
includes({ a: 1, b: 'a', c: NaN }, 'a'); // true
includes('hello world', 'world'); // true
includes('hello world', 'test'); // false