JavaScript Array Partial Search2

by mrrodd

JavaScript

var reporter = function() {
        // sync the target array to the same number
        // of elements as the source
        sync = function(source, target) {
          var itemsToAdd = source.length - target.length;
          
          if(itemsToAdd !== 0) {
            for(var i = 1; i <= itemsToAdd; i++) {
                target.push(target[0]);
            }    
          }
        return target;
        },
        // find the ids for a report given a name(s) or partial name(s)
        find = function(source, filter) {            
            var reports = source.split(','),
                filters = filter.split(','),
                reportIds = "",
                current = "";				
            for(var f = 0; f < filters.length; f++) {
                for(var i = 0; i < reports.length; i++) {
                    if(reports[i].indexOf(filters[f]) > -1) {
                        current = reports[i].split('|');
                        reportIds += current[0] + ',';
                    }
                }
            }
            return reportIds.substring(0, reportIds.length - 1);
        };
    
    return {
        find: find,
        sync: sync
    };
}();

// test find
var ids = reporter.find("QVJ4XA0UBTAG|Voluntary CriticalCare,QVJ4XA0UBSNX|Voluntary Employer","CriticalCare,Employer");
console.log(ids);
// test sync
var strReports = "Voluntary CriticalCare,Voluntary Employer",
    strOppids = "1002",
    arrReports = strReports.split(','),
    arrOppids = strOppids.split(',');

console.log(arrReports, arrReports.length);
console.log(arrOppids, arrOppids.length);

if(arrReports.length != arrOppids.length) {
    reporter.sync(arrReports, arrOppids);
    console.log(arrOppids, arrOppids.length);    
}