Sunday 27 July 2008

Getting intimate with Javascript objects part #7 - Really Useful Associative Arrays

Sorry about the delay getting this post to press. I have been a wee bit busy. However without further ado lets look at a javascript thingie I think is REALLY useful.

You will all be familiar now with the period operator that is used to access the properties of an object. for example myGirlFriend.hair = "Blond (Well the bits you can see)" but you cal also express this as myGirlFriend["hair"] = "Blond (well the bits you can see)".

In C,C++ and Java and object can only have a fixed number of properties that are defined at compile time. Since Javascript is "loosely" this rule is not enforced a program or function can access add or delete properties to any object. When you use the "." operator you have to use the name of the property. The text that follows the "." is not a data type so cannot be manipulated by the program.

[...Drum Roll...]

If we use the [] syntax the identifier is a string, now a string IS a javascript data type and we CAN manipulate strings ... YIPEE!

Say we had a HR data table which had an object like this
Person.Name1 = "Steve"
Person.Name2 = "John"
Person.Name3 = "Marshmallow"
Person.Name4 = "McDonagh"
and we wanted to combine all of this data into a single string for full name would do something like this
PersonFullName= Person.Name1+" "+Person.Name2+" "+Person.Name3+" "+Person.Name4
or we could do it like this

var PersonFullName="";f
or(t=1;t<5;t++)> {PersonFullName += Person["Name"+t]+" ";


This is perhaps a bit simple and not something you consider using in real life but it does demonstrate the use of the [] construct to get at information you may not know ahead of time.

Lets look at a slightly more complicated example. You have an ebusiness selling socks. You do not know ahead of each sale what size or colour or socks your users are going to buy that day.

Say a user comes along and asks for a pair scks that are Yellow and Adult size 9. You have no way using the "." operator to construct and get this information from your stock object which looks like this.

Stock.Yellow7 = 100;
Stock.Yellow8 = 15;
Stock.Yellow9 = 3;

But using the [] construct you can easily do this by concatenating the values entered by the user

alert(Stock["Yellow9"]) would result in an alert that has 3 in it.

When a object's properties are accessed in this way the object has ceased to be just an object it has become an associative array. Don't worry to much about this but you may hear the term used by some smart arse project leader like me and it pays to be up on the jargon we like to use. If you move on to PERL or PHP you will come across objects that can be accessed in this way. An PHP and PERL programmers are much nicer that those legends in their own lunchtimes Java programmers ;-)

Oh an before I forget.. like Lotusscript if you want you can use a For/In construct using the associative array method. Like this, if we wanted a total number of socks
in out Stock object.

var allsocks = 0;
for (Socks in Stock){ allsocks += Socks; }


Since we may add or delete Items from Stock, for example "Yellow Size 7" are just not selling so I give them to the charity shop and remove them from my inventory. If I use the for/in loop to get my total I do not have to change any code that may contain a line like this

allsocks = Stock.Yellow7+Stock.Yellow8+Stock.Yellow9

I use this particular trick nearly everyday and if you are going to be able to jump into the wider world of Javascript in the near future with things like xPages chances are so will you.

Next time (and hopefully very soon) I will post on the properties and methods that are inherited by ALL Javascript objects from the Object class. These methods and properties are often forgotten which is a pity because they can be quite useful!

Disqus for Domi-No-Yes-Maybe