Q:

jQuery / JSON sort results

I have a ColdFusion method getData() which returns a query object as follows:
CustomerCode  ServiceCode  SubscriberCode  Status    UserName
-------------------------------------------------------------
811101        8            gertjan         OPEN      gertjan@blah.net
811101        8            gertjan         CLOSING   gertjan@blah.net
811101        2            99652444        CLOSED    gertjan@blah.net
811101        2            99655000        OPEN      gertjan@blah.net

Note the first two rows - exactly the same except for Status OPEN and CLOSING respectively.
The following function creates a new select option for each row where ServiceCode=8 and Status is either OPEN or CLOSING, which would be the case for both the first two rows.
The data ultimately comes via a web service which is out of my control to change. I need to change the jQuery such that if BOTH an OPEN and CLOSING record exists for the same ServiceCode/SubscriberCode combination, which is the case for the first two rows, then only create an option for the OPEN record.
function getInternetLines(){
        var CustomerCode=global_customerCode;
        var SessionID=global_sessionID;
        var lines=[];
        $.getJSON("/system.cfc?method=getData&returnformat=json&queryformat=column",
               {"SessionID":SessionID,"CustomerCode":CustomerCode}, 
               function(res,code) {
            if(res.ROWCOUNT > 0){
                for(var i=0; i<res.ROWCOUNT; i++) {
                    var ServiceCode = res.DATA.ServiceCode[i];
                    var SubscriberCode = res.DATA.SubscriberCode[i];
                    var Status = res.DATA.Status[i];
                    if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
                        lines.push(SubscriberCode);
                        $('#selInternet').append(
                            $('<option></option>').val(SubscriberCode).html(SubscriberCode)
                        );
                    }
                }
                global_internet_lines = lines;
                if(lines.length == 0){
                    $('#divBroadbandUsage').html('No Active Broadband Connections.');
                }
            }else{
                $('#divBroadbandUsage').html('No Active Broadband Connections.');
            }
        });
    }

HTML
<select name="selInternet" id="selInternet" style="width:120px">
</select>

Any assistance greatly appreciated in getting the cleanest approach to this, without multiple loops of the same dataset, for example.

A:

You would need to keep a hash as you read the data, ignoring data if an 'OPEN' was already found.  Then loop through the hash items and output the data:
if(res.ROWCOUNT > 0){
    var hash = {};  // Hash to store data
    for(var i=0; i<res.ROWCOUNT; i++) {
        var ServiceCode = res.DATA.ServiceCode[i];
        var SubscriberCode = res.DATA.SubscriberCode[i];
        var Status = res.DATA.Status[i];
        if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
             if( hash[SubscriberCode] != undefined && hash[SubscriberCode].status == 'OPEN' ) {
                 // If we already have OPEN, don't load the data
                 continue;
             } else {
                 // Else override whatever data you have for this SubscriberCode
                 hash[SubscriberCode] = { status: Status, subscriber: SubscriberCode, service: ServiceCode }; 
             }
        }
    }

    // loop through the hash and output the options
    for(var x in hash) {
        lines.push(hash[x].subscriber);
        $('#selInternet').append(
            $('<option></option>').val(hash[x].subscriber).html(hash[x].subscriber)
        );
    }

    global_internet_lines = lines;
    if(lines.length == 0){
        $('#divBroadbandUsage').html('No Active Broadband Connections.');
    }
}

I'm not sure what your cases are, but this covers your description I believe.  I realize it is silly to have the hash key stored in the data, but for demonstration, this is how you would retrieve other data.  This code stores Status, SubscriberCode, and ServiceCode, but your example only uses SubscribercCode.  If this is really the case, it is much simpler:
if(res.ROWCOUNT > 0){
    var hash = {};  // Hash to store data
    for(var i=0; i<res.ROWCOUNT; i++) {
        var ServiceCode = res.DATA.ServiceCode[i];
        var SubscriberCode = res.DATA.SubscriberCode[i];
        var Status = res.DATA.Status[i];
        if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
             // If we see the subscriber code, add it to our hash
             hash[SubscriberCode] = 1; 
        }
    }

    // loop through the hash and output the options
    for(var sub in hash) {
        lines.push(sub);
        $('#selInternet').append(
            $('<option></option>').val(sub).html(sub)
        );
    }

    global_internet_lines = lines;
    if(lines.length == 0){
        $('#divBroadbandUsage').html('No Active Broadband Connections.');
    }
}

