In this post I discussed about Arrays in JavaScript. We access elements of JavaScript array using numeric index. For example, if you want to access 3rd element of a JavaScript array, you will do that as following

image

If you want to traverse through each element of the array you can do that in loop. JavaScript also allows you to store and access elements in the array in using string index than numeric index.

image

You can create Associative Array in JavaScript as following,

image

We are assigning values in array using string index. You can access a particular element with string index name. We can access name information from student information as following

image

One problem working with Associative Array is that we can not traverse through array elements in a loop. Associative Array can be best utilized with Array with Arrays is JavaScript. Read here about Array of Arrays

Let us take above example of student array. If we want more than one student to be stored then, we can create student array as following

image

Then we can create associative Student Array as following

image

Now if push student in arrayofStudent array.

image

If we want to access name of the first student from the array, we can do that as following,

image

For your reference code is given below we used in explaining Associative Array.

 <script type="text/javascript" >
 var arrayofStudent=[];
 var student1 = [];
 student1["rollnumber"] = 19;
 student1["name"] = "John";
 student1["grade"] = "A";
 arrayofStudent.push(student1);
 var student2 = [];
 student2["rollnumber"] = 11;
 student2["name"] = "David";
 student2["grade"] = "B";
 arrayofStudent.push(student2);
 console.log(arrayofStudent[0]["name"]);
 console.log(arrayofStudent[1]["name"]);

<span style="font-size: 12pt;"></span>

</script>

<span style="font-size: 12pt;"></span>


In this way you can work with Associative Array in JavaScript. I hope you find this post useful. Thanks for reading.Β 

Advertisements

6 thoughts on “What is Associative Array in JavaScript?

  1. Nice post & really beautifully explained. I got it now.

    I guess there is a small typo “… with Associative Array is that we can traverse through …” , may be it is “..can not..”.

    Thanks for the blog post !

  2. Hello Sir As much i have studied about array in JS, I think there is nothing like associative array in Javascript,
    In JavaScript, arrays always use numbered indexes. If you use named indexes, JavaScript will redefine the array to a standard object.
    ref:-https://www.w3schools.com/js/js_arrays.asp

    please correct me if i am wrong sir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s