Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between objects and associated array in javascript?

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?

like image 594
steveyang Avatar asked Nov 16 '11 02:11

steveyang


People also ask

What is the difference between array and object in JavaScript?

arrray and object in javaScript are same because array are object data type in javaScript. You can use Arrays when you are bothered about the order of elements (of same type) in your collection else you can use objects. In objects the order is not guaranteed.

Is it possible to abuse an array in JavaScript?

Virtually everything in javascript is an object, so you can "abuse" an Array object by setting arbitrary properties on it.Arrays are for numerically indexed data - for non-numeric keys, use an Object. Array" is subclass, or sub-prototype, of "Object". An object of "Object" is not an object of "Array".

What is the difference between array and object performance?

There are plenty of resources on the internet about array vs. object performance, but briefly: array manipulation is slower when you don’t know the index ( linear time, or O ( n )), because you have to iterate over each element until you find the one you’re looking to use.

What is the difference between an array and a properties object?

Here are three main differences between an array and a properties object: A pair of square brackets ( [...]) defines JavaScript's array object. However, we use braces ( {...}) to define properties objects. Above is an array object containing two unnamed values. Above is a properties object containing two properties named month and year.


2 Answers

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.

like image 101
Dave Newton Avatar answered Sep 19 '22 01:09

Dave Newton


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.

like image 41
nnnnnn Avatar answered Sep 19 '22 01:09

nnnnnn