Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative array versus object in JavaScript

Tags:

javascript

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?

like image 334
SAN Avatar asked Nov 09 '11 15:11

SAN


People also ask

What is the difference between associative array and object in JavaScript?

Associated array is defined as key-value pairs which is expressed as the object in JavaScript. The outer object assigned to check has a key pattern and an value of another object. The inner object has keys of name , email ... and corresponding values of regular expression objects.

Is a JavaScript object an associative array?

The key idea is that every Javascript object is an associative array which is the most general sort of array you can invent - sometimes this is called a hash or map structure or a dictionary object. An associative array is simply a set of key value pairs.

Is an object an associative array?

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.

What is the difference between array and object in JavaScript?

Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.


2 Answers

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.

like image 189
Justin Niessner Avatar answered Oct 01 '22 17:10

Justin Niessner


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; 
like image 36
Esailija Avatar answered Oct 01 '22 19:10

Esailija