Maksym D.
Maksym D.

Reputation: 61

inlining spread operator during declaration es2018

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?

Upvotes: 4

Views: 46

Answers (2)

hsz
hsz

Reputation: 152216

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

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

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru,
    uk: {
        ...ru
    }
};

Upvotes: 1

Dan Gamble
Dan Gamble

Reputation: 4155

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

Upvotes: 1

Related Questions