Last active
April 19, 2022 05:29
-
-
Save naramdash/d25230bbbddaacda9931d0966ece395e to your computer and use it in GitHub Desktop.
array helper function
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
function equal<Type, Key extends keyof Type>(propertyName: Key, value: Type[Key]) { | |
return (item: Type) => item[propertyName] === value; | |
} | |
const arr = [ | |
{ id: 1, name: "juho" }, | |
{ id: 2, name: "kim" }, | |
{ id: 3, name: "holy" }, | |
{ id: 4, name: "jesus" }, | |
{ id: 5, name: "fire" }, | |
{ id: 6, name: "water" }, | |
]; | |
const f1 = arr.find(equal("id", 1)); | |
console.log(f1); | |
const f2 = arr.find(equal("name", 'fire')); | |
console.log(f2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment