Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

While "Luke Skywalker" can be accessed by simply object.name, how can I get access to the value of Symbol(id) property of this object?

like image 698
Bakhtiiar Muzakparov Avatar asked Jun 24 '17 07:06

Bakhtiiar Muzakparov


People also ask

How do you find the value of an object object?

Object. values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually is a loop is applied to the properties. Object.

How do you access the properties of an object with a variable?

Answer: Use the Square Bracket ( [] ) Notation There are two ways to access or get the value of a property from an object — the dot ( . ) notation, like obj. foo , and the square bracket ( [] ) notation, like obj[foo] .

How do you access the properties of an array of objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };


4 Answers

That object initializer is invalid, so it's hard to answer.

If that really is a Symbol-named property, the answer depends on whether the Symbol is globally-registered.

If it isn't, you can only discover the symbol via getOwnPropertySymbols. If it's the only one, great, you're in good shape:

const data = {
    name:"Luke Skywalker",
    __typename:"People",
    [Symbol("id")]:"ROOT_QUERY.people."
};
console.log(data[Object.getOwnPropertySymbols(data)[0]]);

That assumes that there's only one Symbol-named property, which we probably shouldn't do. Instead, let's look for the Symbol with the descriptive string "Symbol(id)":

const data = {
    name:"Luke Skywalker",
    __typename:"People",
    [Symbol("id")]:"ROOT_QUERY.people."
};
const sym = Object.getOwnPropertySymbols(data).find(function(s) {
    return String(s) === "Symbol(id)";
});
console.log(sym ? data[sym] : "Symbol(id) not found");

But if it's globally-registered and you know what string it's registered under, you can use Symbol.for to get it:

const data = {
    name:"Luke Skywalker",
    __typename:"People",
    [Symbol.for("id")]:"ROOT_QUERY.people."
};
console.log(data[Symbol.for("id")]);
like image 112
T.J. Crowder Avatar answered Oct 16 '22 19:10

T.J. Crowder


You can use Object.getOwnPropertySymbols() to retrieve it, but this would retrieve all symbols tied to an object. If you want to get that particular symbol on the object directly, you need to store that Symbol object else to be re-used.

const sym = Symbol(id);
const example = {
  name:"Luke Skywalker",
  __typename:"People",
  [sym]:"ROOT_QUERY.people."
}

console.log(example[sym]) //Equals "ROOT_QUERY.people."
like image 34
Nick Wyman Avatar answered Oct 16 '22 18:10

Nick Wyman


Adding to @T.J. Crowder, Symbols can also be discovered through Reflect.ownKeys which will list all object own keys: property names & symbols.

const data = {
    name:"Luke Skywalker",
    __typename:"People",
    [Symbol("id")]:"ROOT_QUERY.people."
};

const sym = Reflect.ownKeys(data).find(s => {
  return String(s) === "Symbol(id)";
});
console.log(sym ? data[sym] : "Symbol(id) not found");
like image 29
Marcos Casagrande Avatar answered Oct 16 '22 20:10

Marcos Casagrande


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]])
like image 25
Yury Tarabanko Avatar answered Oct 16 '22 19:10

Yury Tarabanko