L
L
lasthero672020-04-27 23:48:39
JavaScript
lasthero67, 2020-04-27 23:48:39

How to replace a string with a string from a list?

When loading / updating the site page using the script, I will receive a certain value of the variable. Let's agree that this will be the name of the city, which is located on the site page. For example, I wrote "Moscow" in the variable, but for the script to work further, I need to use the value of the "MSK" variable.
I suppose that I will have a text file with replacements of the following form:
Moscow - MSK
St. Petersburg - St. Petersburg
Yekaterinburg - EKB
, etc.
Please advise how this can be implemented.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-04-28
@Kasperenysh

Make an object...

const sityList = {
    'Москва': 'МСК',
    'Санкт-Петербург': 'СПБ'
}
let sity = 'Москва';
let sityAbr = sityList[sity];
console.log(sityAbr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question