1

I'd like to set one value in an object to another, like so:

var obj = { value1: 'test' value2: value1 } 

Of course that doesn't work.
I feel like there would be an easier way than extending the object. Am I right?

2 Answers 2

1

Try,

var obj = { value1: 'test' }; obj.value2 = obj.value1; 
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is correct way to 'easier way than extending the object' what op wants.
If I'm not wrong, you were missing value2 in the obj :)
@Pavlo Yes, of course. but once the program's flow crosses the second line, the obj would contain a key called value2
0

Another way:

var value = 'test'; var obj = { value1: value, value2: value } 

Be aware that because they point to String values, once value1 is changed, value2 will keep the old string instead of changing with value1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.