0

Let's say we have an object

export const hello = {
 a: 1,
 b: 2
}

and we import it and have a function

import { hello } from ...

function printVarName(x) {
 ...
}

printVarName(hello.b)

How can we convert hello.b to a string 'hello.b'?

The goal to be able to create the string 'hello.b' from the variable x inside of printVarName in this example.

Matt
  • 1,368
  • 1
  • 26
  • 54
  • Using toString() method inside the function? – ivanatias May 13 '22 at 16:21
  • The information of the variable name is lost once it becomes a local variable. Maybe I'll think some workaround and add it as answer – Christian Vincenzo Traina May 13 '22 at 16:22
  • @ivanatias this gives `'[object Object]'` – Matt May 13 '22 at 16:22
  • @CristianTraìna, that's what I was thinking, but a workaround would be great here – Matt May 13 '22 at 16:23
  • 1
    No I think that no workaround is available, the problem is that the object `b` isn't aware that the parent is named `hello`. The only idea that pops in my mind is using a decorator, they are not available in JS (they're a [stage-2 proposal](https://github.com/tc39/proposals#stage-2) though) but they're available in TypeScript – Christian Vincenzo Traina May 13 '22 at 16:29
  • 1
    Btw my idea was about adding the name information as an object metadata using `Symbol()`, you can do it manually like `export const OBJ_NAME = new Symbol(); hello.b[OBJ_NAME] = 'hello.b';` and access it the same way. But it's a pretty tedious task to do everywhere – Christian Vincenzo Traina May 13 '22 at 16:31
  • Oh that's an interesting thought though. I appreciate the outside the box thinking :) – Matt May 13 '22 at 16:36
  • @Matt yes, didn't notice that detail mentioned in the comments above – ivanatias May 13 '22 at 16:42

1 Answers1

1

This is impossible. See the answer to: Determine original name of variable after its passed to a function

There's no way to know what the original name of a variable is after its value has been passed to a function.

The closest thing that I can come up with is the following:

The solution involves installing the dot-prop package that's built to parse strings and find the associated property in an object. https://www.npmjs.com/package/dot-prop

Disclaimer: I have no association to the package itself

It does also involve changing up your implementation since you also now need to pass the object that you're referencing itself.

import { getProperty } from 'dot-prop';
import { hello } from ...

function printVarName(obj, path) {
  const value = getProperty(obj, path);
  
  console.log(path, value);
}

printVarName({hello}, 'hello.b')

We're doing a destructuring assignment in the object provided as the first parameter.

Passing { hello } will pass an object that's the same as { hello: hello } and so when you use the getProperty method it can then find the hello variable and its nested values.

The first parameter is necessary if you're going to be passing variables from outside the scope of the function definition. Otherwise you can create a constant object with all of the variable values and that would let you remove the need for the first parameter where you provide the object.

Abir Taheer
  • 2,502
  • 3
  • 12
  • 34