Skip to content

Instantly share code, notes, and snippets.

@naramdash
Last active April 19, 2022 05:29
Show Gist options
  • Save naramdash/d25230bbbddaacda9931d0966ece395e to your computer and use it in GitHub Desktop.
Save naramdash/d25230bbbddaacda9931d0966ece395e to your computer and use it in GitHub Desktop.
array helper function
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