Multiple key names, same pair value

后端 未结 9 1742
萌比男神i
萌比男神i 2020-12-08 13:33

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 al

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 14:14

    I guess you could do something like this:

    var holidays = {
      'thanksgiving day': {
        foo: 'foo'
      }
    };
    
    holidays.thanksgiving = holidays['t-day'] = holidays['thanksgiving day'];
    

    If you see yourself doing this often or you have more values consider this pattern:

    'thanksgiving, t-day, thanks, thank, thank u'.split(',').forEach(function(key) {
      holidays[key] = holidays['thanksgiving day'];
    });
    

    A better approach would be to process your data beforehand instead of adding duplicates.

提交回复
热议问题