JavaScript Associative Array

JavaScript associative arrays are treated as JavaScript Objects. Objects properties and methods can be assigned arbitrarily.

JavaScript associative array creates seem as like a normal array? No, This way you can't create associative array, you can't use built-in array properties and methods.

Let's see following examples represents array length property is not defined as like a numeric array.

var normalArray= new Array();
normalArray[0] = ".com";
normalArray[1] = ".net";
normalArray[2] = ".org";
normalArray[3] = ".gov";
document.writeln("Length: " + normalArray.length);      // Outputs 4, array length

var associativeArray = new Array();
associativeArray['commercial'] = ".com";
associativeArray['network'] = ".net";
associativeArray['organization'] = ".org";
associativeArray['government'] = ".gov";
document.writeln("Length: " + associativeArray.length); // Outputs 0, array length

Run it...   »

Still, we can access the values through the bracket and . (dot) property notation

document.writeln("commercial domain: " + associativeArray['commercial']);
document.writeln("commercial domain: " + associativeArray.commercial);

Run it...   »

Above examples clearly represent you can't create associative arrays in JavaScript like a normal array. Only way you can create using JavaScript Object.

JavaScript Associative Array

Creating object

An empty object, we can also say the empty associative array is create following two different syntax's,

var myObj = new Object();
var myObj = { }               // both are same

You can store data by keys and value pairs. We can use dot property notation for assigning, accessing, and deleting object values.

You can use either square brackets or dot notation.

var myObj = {};                    // indicates an Object is created
myObj.commercial = ".com"; ".net";
myObj.organization= ".org";
myObj.government= ".gov";

document.writeln(myObj.length);     // results still undefined
document.writeln("commercial domain: " + myObj['commercial']);   // Outputs .com
document.writeln("commercial domain: " + myObj.commercial);      // Outputs .com

Run it...   »

Deleting object

delete statement is preferable for removing properties,

delete myObj.commercial;         // delete value by 'commercial' key

Run it...   »

Object key length

We are working with object, we can get the key length of an object.

We have to use object.keys([object]) method. this method returns the keys. With we should use .length properties to get the length of object key.

Object.keys(myObject);         // ["commercial", "network", "organization", "government"]
Object.keys(myObject).length    // return 4

Run it...   »

In JavaScript, objects (which are essentially associative arrays) create for mapping between key and value pairs. Array keys (as index) to access array values.

var users = [
        {"Name":"Opal Kole", "Address":"63 street Ct."}, 
        {"Name":"Max Miller", "Address":"41 NEW ROAD."}, 
        {"Name":"Beccaa Moss", "Address": "2500 green city."},
        {"Name":"Paul Singh", "Address": "1343 Prospect St"}

document.writeln( users[0] );           // index 0 (object record), object return
document.writeln( users[0]["Name"] );   // index 0, return Name key

Run it...   »

JavaScript does not support multidimensional arrays. But you can archive multidimensional arrays represented by arrays of arrays.

var users = [
      { "Name":"Opal Kole", "Address":"63 street Ct." }, 
      { "Name":"Max Miller", "Address":"41 NEW ROAD." } 
      { "Name":"Beccaa Moss", "Address": "2500 green city." },
      { "Name":"Paul Singh", "Address": "1343 Prospect St" } 

console.log(users[0]);                  // index 0 (object record), object return
console.log(users[0][0]);               // index 0, return first object record
console.log(users[0][0]["Name"]);       // index 0, return name key

Run it...   »