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.

3
  • 1
    Confusing question to the core. Arrays are numerical indexed, while objects are not. Commented Dec 19, 2016 at 20:57
  • Use console.log(map), you will see that the 5 is actually a 'string' key instead of the numbered index like you would see in the array Commented Dec 19, 2016 at 21:00
  • Possible duplicate of Difference between var = {} and var = []? Commented Dec 19, 2016 at 21:21

2 Answers 2

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. :)

Sign up to request clarification or add additional context in comments.

6 Comments

"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 You are right... JavaScript Objects are typically HashMaps.
i wonder if they're computing a hash, and if they keys are subject to a collision.
Key Values will be replaced.
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 ecma-international.org/ecma-262/7.0/… -- 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)
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.