Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple key names, same pair value

Tags:

javascript

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?

like image 419
SomeShinyObject Avatar asked Feb 07 '13 04:02

SomeShinyObject


People also ask

Can object have duplicate keys?

No, JavaScript objects cannot have duplicate keys. The keys must all be unique.


1 Answers

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"     }  }); 
like image 64
Artem Sobolev Avatar answered Oct 02 '22 09:10

Artem Sobolev