javascript function code improvement

Posted on 2011-10-05
Last Modified: 2012-05-12
I’m looking to improve a function call that returns a file name based on three args that are passed.

arg data examples of category, rptGroup & rptItem:

category    rptGroup    rpItem      
Flash             g1           RptItem1
Flash             g1           RptItem2
Flash             g3           RptItem7
Demand       g5           RptItem6

category   > 1-m rptGroups
rptGroups > 1-m rptItems

variables store the file names (var naming represents the category (f = flash, d = demand, etc.), group (g1 = group1, etc.) and report item # which is sequential within a category/group):

var fg1RptItem1 = '/flashgroup1rpt1.pdf';    
var fg1RptItem2 = '/ flashgroup1rpt2.pdf';
var fg2RptItem1 = '/flashgroup2rpt1.pdf';  
var dg1RptItem1 = '/demandgroup1rpt1.pdf';    
var dg1RptItem2 = '/demandgroup1rpt2.pdf';    

Call example:

var category = ‘Flash’;
var rptGroup = ‘g1’;
var rptItem   = ‘RptItem1’;


returns:  rptItem =  '/flashgroup1rpt1.pdf'

function GetFilename(category,rptGroup,rptItem)
   switch(parentCategory+'|'+rptGroup) {

    case 'Flash|g1' :
    //Return appropriate value based on rptItem
    switch(rptItem) {
       case 'RptItem1' : rptItem = fg1RptItem1; break;
       case 'RptItem2' : rptItem = fg1RptItem2; break;  
    } ; break;

    case 'Flash|g2' :
    switch(rptItem) {
       case 'RptItem1' : rptItem = fg2RptItem1; break;
       case 'RptItem2' : rptItem = fg2RptItem2; break;  
       case 'RptItem3' : rptItem = fg2RptItem3; break;  
    } ; break;

    case 'Demand|g1' :
    switch(rptItem) {
       case 'RptItem1' : rptItem = dg1RptItem1; break;
       case 'RptItem2' : rptItem = dg1RptItem2; break;  
    }; break;
   return rptItem;  //returns report file name i.e. '/flashgroup1rpt1.pdf'

Suggestions of an improvement (loop, array, ?) along with a code snippet example based on the above example would be greatly appreciated!

Thank you!
Question by:ssebring
    LVL 75

    Accepted Solution

    Perhaps this?


    var myArgs = {
        Flash: {
            g1: {
            g2: {
        Demand :{
            g1: {
    function GetFilename(category,rptGroup,rptItem) {
       return myArgs[category][rptGroup][rptItem]+'.pdf';
    var category = "Flash";
    var rptGroup = "g1";
    var rptItem  = "RptItem1";

    Open in new window


    Author Closing Comment

    can't thank you enough!  This is an elegant solution that has opened all sorts of shortcuts in the existing code.  Looks like it will be a great way to go with this.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    The viewer will learn how to count occurrences of each item in an array.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now