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
orfoo[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.
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?
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.
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".
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With