Iterate through object properties with Symbol keys

后端 未结 2 723
悲&欢浪女
悲&欢浪女 2021-01-17 12:08

I need to iterate through an object that has Symbols for keys. The following code returns an empty array.

const FOO = Symbol(\'foo\');
const BAR = Symbol(\'b         


        
相关标签:
2条回答
  • 2021-01-17 12:37

    You can iterate over all the keys of Object (String and Symbol keys) with

    Reflect.ownKeys()
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/ownKeys

    0 讨论(0)
  • 2021-01-17 12:44

    Object.values only gets the values of all enumerable named (string-keys) properties.

    You need to use Object.getOwnPropertySymbols:

    console.log(Object.getOwnPropertySymbols(obj).map(s => obj[s]))
    
    0 讨论(0)
提交回复
热议问题