I'm trying to setup an object literal in a JavaScript script that has a key with multiple names. referring to the same object value i.e. something like these that I have already tried:
var holidays: { "thanksgiving day", "thanksgiving", "t-day": { someValue : "foo" } } var holidays: { ["thanksgiving day", "thanksgiving", "t-day"]: { someValue : "foo" } }
Is there a way I can accomplish this?
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
Another approach is to do some postprocessing
function expand(obj) { var keys = Object.keys(obj); for (var i = 0; i < keys.length; ++i) { var key = keys[i], subkeys = key.split(/,\s?/), target = obj[key]; delete obj[key]; subkeys.forEach(function(key) { obj[key] = target; }) } return obj; } var holidays = expand({ "thanksgiving day, thanksgiving, t-day": { someValue : "foo" } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With