Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Trouble with "dictionary" creation in JavaScript

OK, first-off let me tell you that I'm too far from being considered experienced in JavaScript... (perhaps I'm even trying to implement something not actually supported by the language?).

So... here we are...

I want to create a structure like this (it's just an illustrative example) :

Addresses
    --- John
           --- Phone
           --- Address
    --- Peter
           --- Phone
           --- Address

So, I've seen (or perhaps I'm just guessing) that I can do something like that (I've run all my tests in Chrome's JavaScript console) :

var addresses = [];
addresses["John"] = {};
addresses["John"].Phone = "12312312312";
// And so on...

However :


What am I doing wrong? Is there no native support for proper dictionaries (or map - no matter how this thing is called) in JavaScript?


Please note: I want to be able to progressively add entries to addresses, as well as keys to each address entry (and not just create it in one-shot).

Upvotes: 3

Views: 99

Answers (4)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23873

Javascript does not have dictionaries or maps or associative arrays.

It does, however, have objects which can be quite similar. (They can also be quite different, so that is essential to know.)

To make things more confusing, an array is an object. You can put random properties on an array and it will work -- like an object.

Javascript arrays take integer[*] indexes and adjust the .length property.

Objects take string property names. This is important: If you use something that is not a string, it will be converted into one .. usually called [Object object]

Might want to read this: http://eloquentjavascript.net/chapter4.html

[*] Yea, yea, yea -- they are really strings behind the scenes. It is easier to think of them as integers and avoid that grief.

Upvotes: 0

Sandeep
Sandeep

Reputation: 2121

When you do this addresses['somestring'] = {}; you wil get an empty array because {} you are assigning an empty object.

Rather if you do addresses["dd"] = {s: "s"} the addresses will have the object

Upvotes: 1

Quentin
Quentin

Reputation: 944442

JavaScript has Objects and Arrays.

Arrays are a type of Object that treats properties with numerical with special significance. It will have a length property based on the highest value property name (that is a number), and it has methods such as push, pop and forEach to manipulate its numerical members based on their number.

If I do addresses['123'] = {}; then a proper 124-entry array is constructed.

You already have an array. That just adds a member called 123 to it and updates its length.

If I do addresses['somestring'] = {};

This will add that member to the array, but it won't appear in any of the regular means of accessing array members. You have to access it by name.

Don't give named properties to arrays. Use a plain object.

Is there no native support for proper dictionaries

An object is as close as you can get.

If you want something ordered use an array with numerical properties.

If you want something with named keys, use an object.

Upvotes: 2

Guffa
Guffa

Reputation: 700830

You are creating an array, but then you are using it as an object instead (which is possible because an array happens to be an object). To contain properties rather than numbered indices, you should use an object:

var addresses = {};
addresses["Holmes"] = { Phone: "14", Address: "Baker street 221b" };

Upvotes: 7

Related Questions