Skip to content

Instantly share code, notes, and snippets.

@AliBahaari
Last active January 12, 2023 19:00
Show Gist options
  • Save AliBahaari/c53c1eb1cbcdaaf3b81d94d9ec102077 to your computer and use it in GitHub Desktop.
Save AliBahaari/c53c1eb1cbcdaaf3b81d94d9ec102077 to your computer and use it in GitHub Desktop.
A generic function which searches a value in an array of values.
const searchValueInArrays = <T extends any = [], K extends any = "">(
values: T[],
searchedValue: K
): { filteredItems: T[]; searchedValueCounts: number; searchedValue: K } => {
if (values?.length) {
const filteredItems = values?.filter((item) =>
item.includes(searchedValue)
);
return {
filteredItems: filteredItems,
searchedValueCounts: filteredItems.length,
searchedValue,
};
}
throw new Error("The Array Is Empty");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment