variable as index in an associative array - Javascript

前端 未结 7 1271
时光说笑
时光说笑 2021-02-03 23:39

I\'m trying to create an associative array, create an empty array, and then add a (indexName -> value) pair:

var arrayName = new Array;

arrayName[\         


        
7条回答
  •  悲&欢浪女
    2021-02-03 23:50

    At first I don't think you need a real array object to do what you need, you can use a plain object.

    You can simply use the bracket notation to access a property, using the value of a variable:

    var obj = {};
    var nume = var1 + var2;
    obj[nume] = value;
    

    Array's are simply objects, the concept of an "associative array" can be achieved by using a simple object, objects are collections of properties that contain values.

    True arrays are useful when you need to store numeric indexes, they automatically update their length property when you assign an index or you push a value to it.

提交回复
热议问题