Access the value of Symbol(id) property on an object

前端 未结 4 592
太阳男子
太阳男子 2021-02-02 16:22

I have an object fetched from 3rd party API as shown below:

{
    name:\"Luke Skywalker\",
    __typename:\"People\",
    Symbol(id):\"ROOT_QUERY.people.\"
}
         


        
4条回答
  •  情歌与酒
    2021-02-02 16:54

    Symbols were designed to define unique property names to avoid collisions. So you should either have access to the symbol used to construct the object or get all symbols using getOwnPropertySymbols

    const obj = {
      [Symbol('id')]: 1
    }
    
    console.log(obj[Symbol('id')])
    
    const symbols = Object.getOwnPropertySymbols(obj)
    
    console.log(obj[symbols[0]])

提交回复
热议问题