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.

8
  • Using toString() method inside the function? Commented May 13, 2022 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 Commented May 13, 2022 at 16:22
  • @ivanatias this gives '[object Object]' Commented May 13, 2022 at 16:22
  • 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 though) but they're available in TypeScript Commented May 13, 2022 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 Commented May 13, 2022 at 16:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.