associative arrays in JavaScript

associative arrays in JavaScript


What is array? Array is a collection of index items and it is a number indexes.

Some of programming language support array as named indexes and the JavaScript not support the array as name indexes and its provide only number indexes but provide this feature using the associative array.

The array with name indexes are called associative array and the associative array is provide a ways to store the information.

The number index array example as given below

    var users = new Object();
    users["Name1"] = "Anil 1";
    users["Name2"] = "Anil 2";
    users["Age"] = 33;
    alert(Object.keys(users).length); //output is 3.
    var length = Object.keys(users).length;  // 3
   
The name index array example as given below

    var users = [];
    users["Name1"] = "Anil 1";
    users["Name2"] = "Anil 2";
    users["Age"] = 33;
    var length = users.length;         // users.length will return 0
    var detail = users[0];             // users[0] will return undefined


I am going to explain the associate array over the database table columns. A table have some columns and its type. i.e.

The empName as text type, empAge as number type and enpDOB as date type.

If we need to find the type of a column that time we can create the associate array. i.e.

var empDetailType = new Array();

empDetailType["empName"] = "ANil";
empDetailType["empAge"] = 30;
empDetailType["empDOB"] = "10/03/1984";

console.log("Find the emp age type :" + empDetailType["empAge"]);


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^