But I would rather it be on one line though, like this.
JavaScript doesn't care about line breaks. You can write it all in one line:
var strings = { "1": "string 1", "01": "string 1"};
On a more serious note, if you simply want to avoid repeating the value (e.g. to avoid typos when refactoring), then the simplest solution would be to store the value separately in a variable:
const value = "string 1";
var strings =
{
"1": value,
"01": value,
};
If it is the assignment that you don't like duplicating, you could use the fact that an assignment returns the assigned value:
var strings = {};
strings['1'] = strings['01'] = "string 1";
Both if these express the intend in simple and clean ways. Don't try to be too clever, keep it simple.
From the comments however it seems like you want the values of both properties to be "in sync". This is not possible with "normal" property assignment. Doing strings[1] = "new value";
will not change the value of strings['01']
.
To do this you need to use getters and setters (alternatively you can use proxies).
var strings = (function() {
var value = "string 1";
var obj = {};
['1', '01'].forEach(prop => Object.defineProperty(obj, prop, {
get: () => value,
set: v => value = v,
enumerable: true,
}));
return obj;
}());
console.log(strings['1'], strings['01']);
strings['1'] = 'foo';
console.log(strings['1'], strings['01']);
strings['01'] = 'bar';
console.log(strings['1'], strings['01']);