Skip to content

Instantly share code, notes, and snippets.

@amadeuszblanik
Last active April 14, 2020 07:53
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 amadeuszblanik/67b166019c00806ecb7f393ed03a6966 to your computer and use it in GitHub Desktop.
Save amadeuszblanik/67b166019c00806ecb7f393ed03a6966 to your computer and use it in GitHub Desktop.
forEach object in TypeScript
interface ActionTypeProps<P> {
key: keyof P,
value: any,
}
type actionType<P> = (props: ActionTypeProps<P>) => void
type forEachObjectType<P> = (object: P, action: actionType<P>) => void
export const forEachObject: forEachObjectType<{ [key: string]: any }> = (object, action) => {
for (const [key, value] of Object.entries(object)) {
action({key, value})
}
}
import { forEachObject } from "./forEachObject.ts";
const example = {
hello: "world",
foo: "foo",
john: "doe",
city: "london"
}
forEachObject(example, ({key, value}) => {
console.log(key, value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment