Last active
April 14, 2020 07:53
-
-
Save amadeuszblanik/67b166019c00806ecb7f393ed03a6966 to your computer and use it in GitHub Desktop.
forEach object in TypeScript
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
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}) | |
} | |
} |
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
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