Here is an example from a real (not yet working) code:

var a = new Array(); a["www.site.ru"] = "url1"; a["www.site2.ru"] = "url2"; console.log(a); console.log("Длинна массива: "+a.length); 


In the Mozilla Firefox console, I see this picture ...

enter image description here

The question is why 0, because there are elements in the array?

    3 answers 3

    Because it is a hash table (associative array), and not just an array. You need to do so to get 2 (the number of keys):

     Object.keys(a).length 

    Instead:

     a.length 

    Example:

     var a = new Array(); a["www.site.ru"] = "url1"; a["www.site2.ru"] = "url2"; alert("Ключей в ассоциативном массиве: "+Object.keys(a).length); 

      Because adding to an array of elements is done using the push command. The role of associative arrays in Javascript is performed by objects .

        I use indexes in an array

         var a = new Array(); a["www.site.ru"] = "url1"; a["www.site2.ru"] = "url2"; console.log(a); console.log("Длинна массива: "+a.length); 

        What you want to achieve is achieved by objects.

         var a = { "www.site.ru": "url1", "www.site2.ru": "url2"}