TypeScript, Looping through a dictionary

后端 未结 8 834
慢半拍i
慢半拍i 2020-12-04 11:41

In my code, I have a couple of dictionaries (as suggested here) which is String indexed. Due to this being a bit of an improvised type, I was wondering if there any suggesti

8条回答
  •  春和景丽
    2020-12-04 12:14

    If you just for in a object without if statement hasOwnProperty then you will get error from linter like:

    for (const key in myobj) {
       console.log(key);
    }
    
    WARNING in component.ts
    for (... in ...) statements must be filtered with an if statement
    

    So the solutions is use Object.keys and of instead.

    for (const key of Object.keys(myobj)) {
       console.log(key);
    }
    
    

    Hope this helper some one using a linter.

提交回复
热议问题