2
<p style="line-height: 18px; font-size: 18px;  font-family: times;">
Click "<i>Load samples</i>" to view and edit more JS samples.<br>
<br>
Labyrinth generated with JavaScript:<br><br>
<script>
var n = 100;
var sample = [];
for (var i = 0; i < n; i++)
    sample.push({});
console.log(sample.length);
var map = {};
map[5] = 3;
console.log(map.length);
</script>
</p>

Hi all:

I am a New hand in JavaScript and much more familiar to C(C++).

I tested the code above & cannot figure out the meaning of map.

  1. What's the difference if I declare:

    A. map = [];
    B. map = {};
    

    way A seems to be an empty array but B to be an empty object.

  2. Why I can set it as the way of array? (by [] operator such as map[5] = 3).

  3. Why the length of map is undefined?

  4. Could I deem map as a hash table of JavaScript?

Thanks.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
chang jc
  • 489
  • 8
  • 16

2 Answers2

3

Arrays are set of data, identified by consecutive indexed data. While, objects are totally different. They have multiple key value pairs. So, arrays are solely by their indices. Objects are solely by their keys.

var map = [];
map [5] = 5;
console.log(map.length);

The above gives 6 as length because, the consecutive values 0 till 5 are not defined.

While, on the other hand, objects have keys. So to find the length of the objects, we need to use Object.keys function, which will get all the keys in an array.

var map = {};
map[5] = 5;
console.log(Object.keys(map));
console.log(Object.keys(map).length);

The type of the key here, 5 will be stored as "5" (string format). And unlikely to arrays, objects do not have undefined values. They just store the keys. :)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    "In computing, a hash table (hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found." -- from wikipedia -- is the javascript Object using a hash function to store keys? – activedecay Dec 19 '16 at 21:06
  • 1
    @activedecay You are right... JavaScript Objects are typically HashMaps. – Praveen Kumar Purushothaman Dec 19 '16 at 21:06
  • 1
    i wonder if they're computing a hash, and if they keys are subject to a collision. – activedecay Dec 19 '16 at 21:07
  • 1
    Key Values will be replaced. – Praveen Kumar Purushothaman Dec 19 '16 at 21:08
  • 1
    I suppose to answer the OP's 3rd question, you might edit this answer to include the fact that the implementation of Object in the JavaScript engine is implementation dependent http://www.ecma-international.org/ecma-262/7.0/index.html#sec-keyed-collection -- however it is probably OK to assume the OP was asking if it was a hash table. yeah, i guess it's a hash table because it's associating a key with a value. (stopping my anal rant now) – activedecay Dec 19 '16 at 21:14
  • 1
    @activedecay Yea, let's wait for the OP. Looks like OP is so much confused. If needed, I'll let you edit the answer and add it. Let's wait. – Praveen Kumar Purushothaman Dec 19 '16 at 21:16
1

In array, map[5], the 5 here is index, represent the 6th element in the array and it has value of 5, that means you already have 5 elements created before, just values are undefined,

In object, map[5], the 5 here is key, doesn't mean there are other elements created, so the length is 1

lilixiaocc
  • 333
  • 1
  • 15