Multiple key names, same pair value

JSON does not offer such a feature, nor do Javascript object literals.

You might be able to make do with something like this:

holidays = {
    thanksgiving: {foo: 'foo'},
    groundhogDay: {foo: 'bar'},
    aliases: {
        'thanksgiving day': 'thanksgiving',
        't-day': 'thanksgiving',
        'Bill Murrays nightmare': 'groundhogDay'
    }
}

and then you can check

holidays[name] || holidays[holidays.aliases[name]]

for your data.

It’s not a wonderful solution. But it wouldn’t be too difficult to write a little function that created this sort of object out of a representation like:

[
    {
        names: ['thanksgiving', 'thanksgiving day', 't-day'],
        obj: {foo: 'foo'}
    },
    {
        names: ['groundhogDay', 'Bill Murrays nightmare'],
        obj: {foo: 'bar'}
    },
]

if that would be easier to maintain.

Leave a Comment