Skip to content

Instantly share code, notes, and snippets.

@andremury
Last active April 15, 2025 14:11
Show Gist options
  • Select an option

  • Save andremury/47bc1ca00ca3dba2a1be3066095abcc5 to your computer and use it in GitHub Desktop.

Select an option

Save andremury/47bc1ca00ca3dba2a1be3066095abcc5 to your computer and use it in GitHub Desktop.
export const filterMap = <T = unknown, U = unknown>(
arr: U[],
condition: (item: U, index: number) => boolean,
mapTo: (item: U, index: number) => T,
): T[] => {
const newArray: T[] = [];
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (condition(item, i)) {
newArray.push(mapTo(item, i));
}
}
return newArray;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment