Last active
January 12, 2023 19:00
-
-
Save AliBahaari/c53c1eb1cbcdaaf3b81d94d9ec102077 to your computer and use it in GitHub Desktop.
A generic function which searches a value in an array of values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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