Last active
June 11, 2021 15:04
-
-
Save MauricioRobayo/84372051bfa3e637fd02e594cda1b818 to your computer and use it in GitHub Desktop.
Iterating objects https://google.github.io/styleguide/tsguide.html#iterating-objects #gogofast
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
| Iterating objects with for (... in ...) is error prone. It will include enumerable properties from the prototype chain. Do not use unfiltered for (... in ...) statements. Either filter values explicitly with an if statement, or use for (... of Object.keys(...)). |
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
| for (const x in someObj) { | |
| if (!someObj.hasOwnProperty(x)) continue; | |
| // now x was definitely defined on someObj | |
| } | |
| for (const x of Object.keys(someObj)) { // note: for _of_! | |
| // now x was definitely defined on someObj | |
| } | |
| for (const [key, value] of Object.entries(someObj)) { // note: for _of_! | |
| // now key was definitely defined on someObj | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment