8

The Confusing discussion

In this question, there is a discussion on the concepts of associated array and object in javaScript which I got a bit confused.

In this example code:

var check = {
  pattern : {
    name: /^[a-zA-Z-\s]{1,20}$/,
    email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/,
    pass: /.{6,40}/,
    url:  /^[(-)\w&:\/\.=\?,#+]{1,}$/,
    aml:  /<(.+)_([a-z]){1}>$/
    }
};

Here is the discussion makes me confused:

@steven.yang the outer object is not an associative array in your sample, but that is what is being asked for

@sissonb what do you mean by 'outer object is not an associative array'? I think associated array is expressed as object in javascript. The difference is in the notation - either through foo.bar or foo[bar]

@steven.yang associated array means key => value. http://en.wikipedia.org/wiki/Associative_array Your inner object has a key of pattern, the object containing this associative array has no key.

My Understanding of Associated Array and Objects in JS

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.

Could both objects be counted as associative arrays?

1
  • 1
    Associative arrays in JavaScript are just objects used with the array syntax. Commented Nov 16, 2011 at 3:31

5 Answers 5

7

Not really, here's why:

var arr = new Array();
arr["foo"] = 100;
arr["bar"] = 200;
console.log(arr.length); // Prints 0.

Adding elements to an associative array should increase its length (IMO).

It looks and acts (somewhat) like an associative array because of syntactic sugar. What appear to be "array entries", however, are (just) object properties.

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

22 Comments

find me a definition of associative array which states they must have a length
the wikipedia article has no mention of size or length
You also seem to be confusing Array with associative array. I think they are completely unrelated. And an empty associative array (object) does indeed have no own properties to iterate over
@nnnnnn I don't care if the length is computed, or a property. C expects developer-management of array length, which is fine. It's also not OOP, so I wouldn't expect any data structures to have properties, other than what the developer provides. JS is an OOPL, so I expect structures to have meaningful properties. IMO an associative array should have a meaningful length property, and iteration should do what I expect. Again--this is just my opinion; doesn't mean anything in the grand scheme of things :)
@steven.yang I think I got more confused ;)
|
6

If you define "associative array" as a data structure that stores information as a collection of key-value pairs, then yes, JavaScript objects are associative arrays.

However, the phrase "associative array" is not generally used in the context of JavaScript, rather, we say "object". I'd suggest sticking to standard JS terminology to avoid misunderstandings.

Note that JS also has (non-associative) arrays, with elements accessed via numeric indexes. These are also objects and so allow non-numeric key properties, but this is generally considered bad practice.

5 Comments

As soon as it's a non-numeric index, it's an object property, though, just like any other object.
@DaveNewton numeric indexes are also object properties
@Raynos But they're different--using numeric indices does increase the array size, whereas properties don't.
@DaveNewton that doesn't mean they are not object properties. It simply means that Array does magic on the internal [[DefineOwnProperty]] property to make sure length is correct. Specifically ES5.1 15.4.5.1 step 4 where it checks whether the property is an "array index".
@Raynos But I don't care about the semantics. I care about having my expectations met. JS objects do not meet my expectations of an associative array, so I don't consider them an associative array in a meaningful way. Reasonable people can disagree: we disagree, and I'm okay with that. You know my reasons--I believe they're valid, you don't. 'Nuff said.
3

There are no associative-arrays in JavaScript. Everything is object.

Certainly they are similar but associative-arrays in JavaScript are just objects.

9 Comments

Why are objects not assiocative arrays? What definition of associative array do they fail
@Raynos Iterating over the members of an associative array should return only the elements of the associative array. The same is not the case for objects and their properties.
terminology...certainly you could say associativeArray = Object and Object=associativeArray in JavaScript, but in my opinion they are all just Object.
@DaveNewton depends of your definition of iteration. I'd argue iterating over the own properties does exactly what you want. The fact you can iterate over non-own properties does not make it a non-associative array
@JohnHartsock everything is both ;)
|
3

Associative Array

In computer science, an associative array (also called a map or a dictionary) is an abstract data type composed of a collection of (key,value) pairs, such that each possible key appears at most once in the collection.

As far as I know objects in JavaScript match that definition.

Of course there is no unique "Associative Array" object, that's any different then any other normal object. So if you want associative array functionality use a javascript object.

However the following is a common piece of misinformation

There is no associative array in JavaScript

You should simply ignore these people, maybe try to convince them they are wrong.

10 Comments

How about this? "There is no unique associative array data type in javascript that is any different than an object". If you want the behavior of an associative array in javascript, use a javascript object.
Rather then a -1 can you please identify what criteria of "Associative Array" objects fail
I'm not criticizing your post nor did I vote it down. I was just suggesting an enhancement. I think people get confused in javascript because they are expecting a data type called an "associative array" and don't realize that an object has all those capabilities. Then, they use an "array" and get further confused when .length remains zero even though they have added items to the array.
@TimDown meh, if people don't have a standardized definition for terminology it would be annoying. If I ask for an apple and you give me an orange, then insist that orange is an apple, I will rage.
@c69 because it satisfies the definition of an associative array.
|
0

My response is coming late, but I hope this can help people to understand this difference. I was reading this post and there isn't a satisfactory definition of an associative array. JavaScript doesn't have Associative Arrays. These are just objects that you can treat as associative arrays for convenience. In objects you store values as named properties, very similar to associative arrays in other programming languages. That's why you can access the properties like an associative array, but with methods associated to objects. You can test creating an object and try all the things you can do with an associative array, but you can't do all the methods of an array.

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.