How to create an associative array in JavaScript literal notation

前端 未结 6 820
再見小時候
再見小時候 2021-01-30 16:39

I understand that there are no associative arrays in JavaScript, only objects.

However I can create an array with string keys using bracket

6条回答
  •  情书的邮戳
    2021-01-30 17:29

    You can use Map:

    var arr = new Map([
       ['key1', 'User'],
       ['key2', 'Guest'],
       ['key3', 'Admin'],
    ]);
    
    var res = arr.get('key2');
    console.log(res); // The value is 'Guest'
    

提交回复
热议问题