easy way to shortcut/point to a dictionary?

Because the data I am working with, the dictionary key "Courses" may contain (1) an array of [multiple] dictionary value(s), or (2) one dictionary value, in other words, not an array.  This means at a few points in my code I need to go about accessing the data like the following:

// earlier in a different function multipleCourses is determined, depending upon
// whether the data contains more than one course

function getCourseValue (courseIndex) {  
// courseIndex is known if there are multiple Courses, but I need to write my functions
// this way to handle both possible cases, i.e., if only one course or more than one

    if (multipleCourses) {
        System.println( loadedData["data"]["courses"][CourseIndex]["name"].toString() );
    } else { 
        System.println( loadedData["data"]["courses"]["name"].toString() );
    }
}

As you can see, this is clunky.  I would like to simplify things, save memory and make things quicker.  Is it possible to declare a variable/pointer to refer/point to a sub-dictionary, so I no longer need to differentiate between the single and multiple cases?  For instance, lets say after multipleCourses is determined, and I know I am no longer concerned about the other courses, but now all I want to focus on is on course 3... could I define a variable like the following, and save having to use the courseIndex or the multipleCourses variable throughout the rest of the program?

var chosenCourse = loadedData["data"]["courses"][3];
    
// since at a point in my program the user has chosen one course, so
// we no longer care if there are multiple courses, or even about the 
// other courses.  So I declare this shortcut variable to access the data easier

// access Course Value in an "inline" manner, looks much cleaner,
// and no longer has to use multipleCourses and indexCourse to access 
// the proper data... 
System.println( chosenCourse["Name"].toString() );

can anyone tell me if this is possible using Monkey C?  thanks in advance for the feedback!   ehh, I'm tired!!!

  • Arrays can have one item.

    Only store arrays.

  • Thank you.  Yes, this is a good solution, and I may implement something like this.  I suspect I will be able to simplify things greatly.

    However so far I had written my program this way, because my app downloads JSON data which is in a 3rd party format which I cannot change, and so I'd like my App to be able to keep things the same with that format.  

  • Get the JSON and create an array item from it. 

    That lets you deal with the odd case in one place rather than every place you access the dictionary.

    You'll have to deal with the array with one item thing in any case.

    (In the long run, you'll be happier modifying your program now to do this the "right way".)

  • Yes, you can do exactly what you suggest in the original post. For efficiency reasons, it is probably best to do this because it reduces the need to do a bunch of dictionary searches.

    That said, you could also just convert the single record case into the array case when you receive the data and then remove all of the if/else code throughout your application.

    function onResponse(code, data) {
    
        if (code == 200) {
        
            var courses = data["data"]["courses"];
            if (courses != null) {
            
                // this could have been combined into the conditional
                // above but the forum software chokes on that
                var isArray = (courses instanceof Array);
                if (!isArray) {
                    data["data"]["courses"] = [ courses ];
                }
            }
        }
        
        // now we know data["data"]["courses"] is either null or an array
    }