How to store a key-value form similar to hashmap in JS/Jquery

Define a writing method similar to HashMap in JS

I believe that you will often encounter some data fields that need to be translated during the development process of the previous page.
Of course, writing a conversion function is the most common way, but today I will teach you a new method, which is to define a json string to translate.

code block

code show as below:

var map = {
    '部门1':'部门经理1',
    '部门2':'部门经理2',
    '部门3':'部门经理3',
};

At this time, if you want to find out the manager of "Department 1", you can use the following method to get it:

console.log(map.部门1);

输出: 部门经理1

Some would say, what if "department 1" is a variable?

Then you have to use the array method to get it, as follows:

var department = ‘部门1’;

console.log(map[department]);

输出: 部门经理1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524118&siteId=291194637