4

I want to do something like this:

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru: {
        statistics_title: 'Статистика'
    },
    uk: {
        ...ru
    }
}

obviously it doesn't work, of course, I can set another const, and then use it but I'm lazy ) so I'm curious if javascript allows you to do so?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Maksym D.
  • 61
  • 4

2 Answers2

1

You can't self reference in objects unless it's a method.

You could do:

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru: {
        statistics_title: 'Статистика'
    },
    get uk () {
        return this.ru
    }
}

Edit

If you want intending for translations.uk to not be a copy of ru and hold it's own data this way won't work. This just sets up translations.uk as a reference to translations.ru

Dan Gamble
  • 3,965
  • 1
  • 24
  • 44
1

You have to declare this ru value before spreading, i.e.:

const ru = {
    statistics_title: 'Статистика'
};

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru,
    uk: {
        ...ru
    }
};
hsz
  • 148,279
  • 62
  • 259
  • 315