Created
November 23, 2022 03:14
-
-
Save rimzzlabs/700d3525731bda0cb6646e9a2b27eafe to your computer and use it in GitHub Desktop.
This file contains 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
type Value = Record<string, unknown> | |
export const arrayObjSearch = <T extends Value>(arr: Array<T>, key: keyof Array<T>[0], target: string): T | null => { | |
if (!key) throw new TypeError('You should provide what key to find') | |
if (arr.length === 0) return null | |
let i = 0 | |
do { | |
const source = arr[i][key] | |
if (source === target) return arr[i] | |
i++ | |
} while (i < arr.length) | |
return null | |
} | |
export const arrayObjIndexSearch = <T extends Value>( | |
arr: Array<T>, | |
key: keyof Array<T>[0], | |
target: string | |
): number | null => { | |
if (!key) throw new TypeError('You should provide what key to find') | |
if (arr.length === 0) return null | |
let i = 0 | |
do { | |
const source = arr[i][key] | |
if (source === target) return i | |
i++ | |
} while (i < arr.length) | |
return null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment