Iterate through object properties with Symbol keys

Dav*_*haw 3 javascript symbols

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('bar');

const obj = {
  [FOO]: 'foo',
  [BAR]: 'bar',
}

Object.values(obj)
Run Code Online (Sandbox Code Playgroud)

How can I iterate the values in obj so that I get ['foo', 'bar']?

Ber*_*rgi 9

Object.values 仅获取所有可枚举的已命名(字符串键)属性的值。

您需要使用Object.getOwnPropertySymbols

console.log(Object.getOwnPropertySymbols(obj).map(s => obj[s]))
Run Code Online (Sandbox Code Playgroud)