Created
August 25, 2022 23:19
-
-
Save zettca/51e09d83b47f2d518f7784901ac51d90 to your computer and use it in GitHub Desktop.
Delete an object's entry. Doesn't mutate.
This file contains hidden or 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
| export const deleteObjectKey = <T extends Record<string, any>>( | |
| obj: T, | |
| path: string | |
| ): T => { | |
| const newObj = { ...obj }; | |
| const paths = path.split("."); | |
| paths.reduce((acc, key, idx) => { | |
| if (idx === paths.length - 1) { | |
| delete acc[key]; | |
| return true; | |
| } | |
| return acc[key]; | |
| }, newObj); | |
| return newObj; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.