How to create an associative array in JavaScript literal notation

前端 未结 6 712
再見小時候
再見小時候 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:15

    Arrays and Objects are different things.

    For an object, you can do things like:

    var obj = { "property1": "value1" };
    obj.property2 = "value2";
    

    For arrays, you have to do this:

    var arr = [];
    arr[0] = "value";
    

提交回复
热议问题