JavaScript Data Structures - Array Object
JavaScript Data Structures - Array Object
Written by Ian Elliot   
Thursday, 05 January 2017
Article Index
JavaScript Data Structures - Array Object
Acess Methods
Array v Associative Array

Array and associative array

Now we come to a little controversy.

The Array object is still an object and as such it can be used as an associative array.

The associative nature of the Javascript Array is supplied via inheritance from the more general Object and it can be argued that it is accidental.

Every object in Javascript is an associative array, but the Array object is crafted to work with integer indices - so don't use it for an associative array. After all, its additional facilities don't work well with the basic associative features.

For example if you try:

myArray["A"]=0;
myArray["B"]=1;
myArray["C"]=2;
alert(myArray.length);

you will discover that length is zero. The point is that integer properties are treated differently from associative properties.

There is also no direct way of creating an associative array via a literal. for example while you can write:

var A={A:0,B:1,C:2};

This creates an object A.

If you try:

var A=[A:0,B:1,C:2];

then you get an error because an Array literal can't be declared as an associative array.

Similarly:

var A=["A","B","C"];

declares a standard Array indexed as a[0]="A" and so on.

However

var A={"A","B","C"};

just generates an error message because objects need name value pairs to initialise them.

So the argument goes that the Array object is not to be used as an associative array because it was created to act as an integer indexed array.

However things aren't quite this simple.

Consider the way that you can reference object properties following:

var A={A:0,B:1,C:2};

You can refer to property B in either of two ways A.B or A["B"]. This is strange because [] is the array dereferencing operator and you can use this on any object, not just an Array object.

This confuses the issue.

However you can't refer to an array element using property notation even though the indices are stored as valid object properties. That is, A.0 doesn't work even though A does have a property called numeric zero.

The best way way to understand all this is to realize that an Array object is an associative array that has integer valued properties and this makes it special. The best advice is - don't use an Array object as a general associative array unless you need to mix string and numeric indices.

In general an Array is an array and an Object is an associative array.

 

datastruct

 


JavaScript Data Structures 

Cover

Contents

  1. The Associative Array
  2. The String Object*
  3. The Array object
  4. Typed Arrays I*
  5. Typed Arrays II*
  6. Speed dating - the art of the JavaScript Date object
  7. Doing JavaScript Date Calculations*
  8. A Time Interval Object*
  9. Collection Object
  10. Stacks, Queue & Deque
  11. The Linked List*
  12. A Lisp-like list
  13. The Binary Tree
  14. Bit manipulation*
  15. Active Logic, Truthy and Falsey *
  16. Master JavaScript Regular Expressions

 * First Draft

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

Banner
 


JavaScript Async - The Fetch API

There are a number of new features in JavaScript that make good use of promises and hence async and await. The Fetch API is a replacement for the XMLHttpRequest function and perhaps the jQuery Ajax fu [ ... ]



Getting Started with Node.js

By bringing JavaScript to the server, Node.js is something of a buzz in the wider JavaScript world. Here we look at the problem it solves and how to make good use of it.


Other Articles

 
 

 

blog comments powered by Disqus

<ASIN:0596806752>

<ASIN:0321812182>

<ASIN:1491901888>

<ASIN:144934013X>

<ASIN:193398869X>

<ASIN:1449373216>

 



Last Updated ( Thursday, 05 January 2017 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.