Arrays and Objects in JavaScript

by Jagadish Pulakhandam on 8/7/2012 6:49:07 AM
Rated 0 from 0 votes
Brief: Demonstrates on creating arrays and accessing elements of an array as properties of an object
Posted to: Advanced JavaScript Programming/Development tutorials/videos- Object Oriented Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached source code/video demonstrates the following:
  • How to create associative arrays in JavaScript
  • How to access elements in an associative array
  • How to access elements of an associative array with object notation
Associative arrays: Arrays which have indices (positions of elements in the array) as strings
Object: A variable which will have more than one value.  Each value is stored (and accessible) through a (property) name.  An array is also treated as an object JavaScript

Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script type="text/javascript" language="JavaScript">
07.        //DEMO: Objects are indeed arrays internally
08. 
09.        var ary = []; //define an array variable
10. 
11.        //add elements to array with key as index (Associative array)
12.        ary["Empno"] = 1001;
13.        ary["Ename"] = "Jag";
14.        ary["Sal"] = 5400;
15.        ary["Deptno"] = 10;
16. 
17.        //display the values (classic way)
18.        alert(ary["Empno"].toString() + ", " +
19.                ary["Ename"] + ", " +
20.                ary["Sal"].toString() + ", " +
21.                ary["Deptno"].toString()
22.        );
23. 
24.        //display the values using object.property notation
25.        alert(ary.Empno.toString() + ", " +
26.                ary.Ename + ", " +
27.                ary.Sal.toString() + ", " +
28.                ary.Deptno.toString()
29.        );
30. 
31. 
32.    </script>
33.</head>
34.<body>
35. 
36.</body>
37.</html>



Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following