Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created November 5, 2020 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/ac28ff31f3633dd6ce81777da51f3c18 to your computer and use it in GitHub Desktop.
Save jonathantneal/ac28ff31f3633dd6ce81777da51f3c18 to your computer and use it in GitHub Desktop.
filteredObject: Returns a new object with all entries that pass the test implemented by the provided function.
/** Returns a new object with all entries that pass the test implemented by the provided function. */
function filteredObject<T>(object: { [s: string]: T } | ArrayLike<T>, predicate: (value: T, name: string, array: [string, T][]) => unknown): { [s: string]: T } {
const newObject = Object.create(Object(object).prototype)
const entries = Object.entries(object)
for (const [name, value] of entries) {
if (predicate(value, name, entries)) {
Reflect.set(newObject, name, Reflect.get(object, name))
}
}
return newObject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment