0

I was trying to create an object, where it would be nice (and save me some typing and duplication), if I could reference the value's key name inside the declaration, e.g. something like below:

const object = {
  "keyNameA": doSomething(`${this values' key name}`), //i.e. doSomething("keyNameA")
  "keyNameB": doSomething(`${this values' key name}`)  //i.e. doSomething("keyNameB")
}  

Question is: Is that something that can be achieved in JS with an object literal declaration?

Thank you in advance!

(I know that with a constructor function you can probably achieve the above, this is mainly about the question, if it is possible via object literal as well, somehow)

pano9000
  • 1
  • 1
  • 4
    No, that's not possible. You could take an array of key names, map it to `[key, value]` pairs and create the object via `Object.fromEntries`. – Felix Kling Jan 11 '23 at 08:20

0 Answers0