Creating some associative array in JavaScript

后端 未结 2 1217
孤城傲影
孤城傲影 2020-12-21 13:24

I have an object which contains an array, and I want it to contain an associative array so keys are strings. How can I do it?

This does not work:

{prof         


        
2条回答
  •  抹茶落季
    2020-12-21 13:57

    JavaScript doesn't have "associative arrays".

    It has objects, which you can store values on in named properties.

    It has arrays, which store an ordered set of values via integer properties. Since they are a type of object, you can also store named values on them (but you can't create them using the literal syntax, only add them after the array is created) but this isn't considered good practise and they will be ignored by just about anything that has special case handling for arrays.

    As of ES6 it also has Maps which are similar to objects in that you can give them named values, but also to arrays in that order is preserved. There is no literal syntax for creating a Map.

提交回复
热议问题