SageFix. LetsFix!  
Current Location : Home > Web Development > JavaScript > Overloading Arrays for multi-dimensional purposes > Post Share/Save/Bookmark    

Main Menu
SageFix - Let's Fix

Board Categories
    Web Site Designing
- Graphics
    Web Development
- .NET
- Classic ASP
- JavaScript
- PHP
    WebSite Management
- Promotion Techniques
- Search Engine Optimization
    Databases
- MySQL

Tutorials

Database Connection Strings

  Contact Us


Overloading Arrays for multi-dimensional purposes      Jul 26, 2002, 04:14 990447
  
VARIABLE1,VARIABLE1,PROPERTY1,PROPERTY2...PROPERTY6|/

You have a number of lines - in the above format.
They are in a single variable.
You want to take it
and create a multi-dimensional Array.
MORE than that - you want it more in an hash ( list, dictionary ) format.

I thought it couldn't be done in JS.
I finally hit a problem which i couldn't overcome without
working out how to handle this.

For anyone that doesn't know how these look:
Code:
[VARIABLE 1a]    | - [VARIABLE 2a] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 3a] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 4a] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 5a] = [ PROPERTY 1, PROPERTY 2 ...][VARIABLE 1b]    | - [VARIABLE 2b] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 3b] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 4b] = [ PROPERTY 1, PROPERTY 2 ...]    | - [VARIABLE 5b] = [ PROPERTY 1, PROPERTY 2 ...]
Now you want to be able to access these by name but not
have multiples, and not overwrite the previous value.

The method I've ended up with looks like this:
Code:
MAKES = new Array();TYPES = new Array();var str='/ECMN,AVIS,FORD,MONDEO,GREEN,1.5|/ECMN,HERTZ,FORD,MONDEO,GREEN,1.5|/ECAR,HERTZ,FORD,MONDEO,GREEN,1.5|/';var all = str.split(//|/);for (i=0;i<all.length;i++){        var set = all[i].split(/,/);        if (!TYPES[set[0]]){ TYPES.push(set[0]); TYPES[set[0]] = 1; }        if (!MAKES[set[1]]){ MAKES.push(set[1]); MAKES[set[1]] = 1; }        l=set[0]+"['"+set[1]+"']=new Array('"+set[2]+"','"+set[3]+"','"+set[4]+"','"+set[5]+"')";        try { eval(l); }        catch(e) { eval(set[0]+' = new Array();'); eval(l); }        }alert(eval(TYPES[1]+'["'+MAKES[1]+'"]'));alert(MAKES);
Effectively this gives you the ablility to say:
alert ( ECMN['AVIS'] ) and you'll get an array returned
containing Ford Mondeo Green 1.5!

Now i don't know if i'm being stupid - but to me that
seems like a very slight breakthrough.

Am i rediscovering the wheel, and is there a common way of doing this?

Can anyone think of how to improve this?

Flawless

 









Subscribe to our mailing list
email


Current Location : Home > Web Development > JavaScript > Overloading Arrays for multi-dimensional purposes > Post Share/Save/Bookmark    

Development problem? SageFix is completely free -- paid for by advertisers and donations. Click here to Contact Us If you have any query. Enjoy!
Request processed in 7.0E-5 seconds Home - Contact Us - Terms Of Service - Privacy Policy - Copyrights - Top
Advertisements do not imply our endorsement of that product or service.
Current server time now is 06-Sep-2010 07:08:28
Copyright © 2009 Sagefix Inc. All rights reserved.
Powered By SageFix