Does Javascript have associative arrays?

前端 未结 6 1033
[愿得一人]
[愿得一人] 2020-12-07 02:43

Does Javascript have associative arrays? Please explain.

6条回答
  •  渐次进展
    2020-12-07 03:20

    Instead of associative arrays. Javascript has objects. Properties of an object are addressed using a string.

     var obj1 = {};  // declare empty object
     var obj2 = {a: 1, b: 'string', c: [4,5]}; // obj with 3 properties, a, b, and c
            // note that the 'c' property contains an anonymous array 
    
     alert(obj2.a); // shows 1
     obj2.a = 'another string'; // redefine the 'a' property
     obj2.cookie = 'oatmeal'; // add a new property to the object
     obj2['ice_cream'] = {vendor: 'Beyers', 
                          flavor: 'Chocolate Surprise'}; // add anonymous object as
                               // a new property for the object
    
     assert(obj2.a === obj2['a']); // two ways to retrieve the value
     var i = 'a'; // using an index varable
     assert(obj2.a === obj2[i]);  // note the i does not have apostrophes around it
    

    See the Quirksmode docs

提交回复
热议问题