72

In my script there is a need to create a hash table, and I searched in google for this. Most of the folks are recommending JavaScript object for this purpose. The The problem is some of the keys in the hash table have a "." in them. I am able to create these keys easily with the associative arrays.

I don't understand why associative arrays are bad. The first thing that is mentioned on the sites that I looked at is the length property.

I am coming from the Perl background, where I used hashes. Most common uses were to get the value from a key, check if a key exists, delete a key-value pair, and add a key-value pair. If these are my common uses, can I safely use an associative array?

2
  • 2
    What is this associate array that you speak of? Commented Nov 9, 2011 at 15:54
  • 6
    Beware, several of the answers below neglect to mention the problem of keys colliding with builtin methods: see devthought.com/2012/01/18/an-object-is-not-a-hash Commented Apr 19, 2013 at 5:11

7 Answers 7

105

In JavaScript, objects are associative arrays...there aren't separate concepts for them. You are also able to safely use '.' in a key name, but you can only access the value using the bracket notation:

var foo = {}
foo['bar'] = 'test';
foo['baz.bin'] = 'value';

alert(foo.bar); // Shows 'test'
alert(foo['baz.bin']); // Shows 'value'

If you're using them already and they work, you're safe.

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

6 Comments

Note that opposed to e.g. PHP associate arrays, the objects properties are not sorted, i.e. you can't rely on their order.
I don't think you could generalize objects as associative arrays, mainly because objects won't have the methods that associative arrays have implemented. (Example: map, reduce, filter, etc.)
@hlin117 - Associative arrays (objects) don't have those methods either. Only plain Arrays have those methods.
@JustinNiessner in my node application I do have map, reduce etc. for an associative array.
the problem is that foo.length=0
|
34

In a JavaScript, an object and array are pretty much the same thing, with an array having a bit of magical functionality (autoupdating the length property and such) and prototype methods suitable for arrays. It is also much easier to construct an object than using an associative array:

var obj = {"my.key": "myValue"};

vs.

var obj = [];
obj["my.key"] = "myValue";

Therefore never use the array object for this, but just the regular object.

Some functionality:

var obj = {}; // Initialized empty object

Delete a key-value pair:

delete obj[key];

Check if a key exists:

key in obj;

Get the key value:

obj[key];

Add a key-value pair:

obj[key] = value;

7 Comments

what about to get size of an object ?
@realtebo there is no first-class syntax for that, you can make a function for it if you need
The part of this answer about not using an array as an associative array because it takes two lines of code instead of one is pretty misleading. That construct doesn't populate the target element, it sets a property of the array, exactly as described in the post at the same time by @Dominic Goulet and NullUserException, which IMO is the correct answer. The only reason it behaves at all like an associative array (you can reference obj["my.key"]) is that the array's properties are an object, and objects are as close as Javascript comes to having associative arrays.
I don't think it's correct to say that an array and an object are the same thing in JS. I mean, by that standard practically everything in JS is about the same thing as an object. An array allows you to set arbitrary properties just like any other type of object. However, an object by itself has no concept of ordered data.
It's not so much that "object[s] and array[s] are pretty much the same thing" as it is that Arrays are Objects (but not vice-versa).
|
19

Because there is no such thing as built-in associative arrays in JavaScript. That's why it's bad.

In fact, when you use something like:

theArray["a"] = "Hello, World!";

It simply creates a property called "a" and set its value to "Hello, World!". This is why the length is always 0, and why the output of alert(theArray) is empty.

2 Comments

Sure there's such thing as an associative array in javascript. There just isn't a built-in concept of an associative array. For example, closure has an associative array: docs.closure-library.googlecode.com/git/…
@JasonBaker The OP was referring to javascript (see tag), not google libs. Of course you can do whatever you want in javascript, and as you stated there is no such thing as builtin associative arrays in javascript. I thought that was obvious, but I'll update answer.
9

Actually, an "associative array" is pretty much the same as an "array-like object" in ECMAScript. Even arrays are objects in ECMAScript, just with the exception to have numeric keys (which are still strings in the background) and a .length property, along with some inherited methods from Array.prototype.

So, a Perl hash and an ECMAScript object behave similarly. You might not know that you can access object properties not only via a dot, but also with brackets and strings, like

var myObj = { foo: 42 };

myObj.foo; // 42
myObj['foo']; // 42

Knowing that, you can also use keys with .

var myObj = { };
myObj['hello.foo.world'] = 42;

Of course, you can access that key only with the bracket notation.

Comments

8

You can use . in key names on JavaScript objects (AKA associative arrays) if you'd like; they're accepted without issue. The minor drawback is you can't use shortcut notations with the dotted keys, e.g.

var x = {};
x['hello'] = 'there';
alert(x.hello);

is perfectly acceptable and will pop up an alert with 'there' in it. But if you use a dotted name:

var x = {};
x['this.is'] = 'sparta';
alert(x.this.is);

will fail, as JavaScript will look for an attribute named this in the x object, which does not exist. There is only the this.is attribute.

1 Comment

Completely in favor of replacing the old Foo Bar for "THIS IS SPARTA"
3

There isn't an associative array. It's just an object.

foo.bar;    // Equivalent to...
foo["bar"]; // Looks like associative array.

4 Comments

Why does JavaScript permit both? Is there any actual difference between them?
Why shouldn't it permit both? The only real difference is that you can use variables using "array notation", e.g., bar = 'plugh'; foo[bar] would return the value referenced by foo.plugh.
It just seems blatantly redundant to me (unless I'm missing something) - stackoverflow.com/questions/42513889/…
@EJoshuaS My only response is "so what?" Dot access is cleaner, bracket access allows other constructs. I don't see an issue with the "redundancy" (in quotes because they allow different uses). ¯_(ツ)_/¯
2

For the sake of convenience of using data, there should be no difference between an object and an array. You can think of it as an object or you can think of it as an associative array. At the end, you can just think of everything as data.

  • For PHP, [ ] accepts 0, 1, or more items(array), and it is called an associative array. It is JSON in PHP's coat:

    $data = ["message"=>[ "id"=>405, "description"=>"Method not allowed.", "detail"=>[]], "object" => []];

  • For JavaScript, { } accepts 0, 1, or more items(array), and it is called an object. This data format is JSON:

    data = {"message": { "id":405, "description":"Method not allowed.", "detail" : {}}, "object" : {}};

I just call them data. The simplest way to describe data is JSON, or its variants.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.