2

Is there any way to access the value of a key of an object in the same object?

Something like this:

const myObj = {
    keyOne: 'value',
    keyTwo: this.keyOne + 'another value'
})

I know that this may be a previously asked question, but I didn't found any answer to this problem.

Alex Shavkuta
  • 73
  • 1
  • 6

2 Answers2

1

You can use a getter:

const myObj = {
    keyOne: 'value',
    get keyTwo() { return this.keyOne + 'another value' }
};

console.log(myObj);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

const myObj = {
    keyOne: 'value',
};
myObj.keyTwo = myObj.keyOne + " an custom text";

console.log(myObj);
S B RAKESH RATH
  • 443
  • 3
  • 10