variable as index in an associative array - Javascript

前端 未结 7 1437
时光说笑
时光说笑 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:53

    You would use objects to do that:

    var hash = {}
    hash["foo"] = "foo";
    hash.bar    = "bar";
    // This is the dynamic approach: Your key is a string:
    baz         = "baz"
    hash[baz]   = "hello";
    

    To iterate, just use a for loop or $.each() in jQuery.

提交回复
热议问题