The key value in the object uses the method of variable declaration

Table of contents

1. Problems

Two, the solution

3. Summary


1. Problems

1. I always thought that the key value of an object cannot be declared with a variable. Writing this way the compiler will report an error:

        obj = {

          item.key[0]: [e[0]],

          item.key[1]: [e[1]]

        };

Two, the solution

1. Today, I saw the code written by the boss and suddenly found that it can be done, just put the variable in the square brackets !

        obj = {
          [item.key[0]]: [e[0]],
          [item.key[1]]: [e[1]]
        };

3. Summary

1. It's amazing, it seems that people really need to keep learning to surpass themselves! ! ! ! ! !

2. obj={[varKey]:value}

/*

Hope it helps you!

If there is any mistake, welcome to correct me, thank you very much!

*/ 

Guess you like

Origin blog.csdn.net/qq_45327886/article/details/129611321