I've provided a snippet that you can run in your console to test the functionality, and created a demo function that you could use forthwith (removing console.log statements). It returns an array of company names.
Effectively what I'm doing is using the fact that Javascript has native associative arrays for objects, so I am assigning the toLowerCase
fieldname (in your case the company) as the field for the associative array lookup point. If the fieldname is not already a property, then this is the first time we've added it. On the first time we add one (consider "bobo") we set it to zero. On the subsequent times, we increment it by one.
function getCompaniesOver(companyArray, discountMinimum){
var tallyObject = {},
retArray = [],
has = Object.prototype.hasOwnProperty; //I'm making sure that we have a clean reference to the hasOwnProperty
for(var k in companyArray){
var s = companyArray[k]+''; s = s.toLowerCase();
if (has.call(tallyObject,s)){
tallyObject[s]++;
} else {
tallyObject[s] = 0;
}
}
console.log(tallyObject); // for debugging insepection.
console.log('companies with ' +companies_eligible_for_discount+ ' number of employees above 1 attending')
console.log('--------')
for (var k in tallyObject){
if (tallyObject[k] >= companies_eligible_for_discount){
console.log(k);
retArray.push(k);
}
}
console.log('--------')
return retArray;
}
var company_names_long = ['acme', 'acme', 'bobo', 'comanche', 'acme', 'comanche', 'comanche', 'acme', 'sanford & sons', 'Sanford & Sons', 'Johnson&Johnson', 'johnson&johnson'];
var company_names = ['acme', 'acme', 'bobo', 'comanche', 'acme', 'comanche'],
companies_eligible_for_discount = 2; //this is the range you can supply
getCompaniesOver(company_names, companies_eligible_for_discount );
companies_eligible_for_discount = 1;
getCompaniesOver(company_names_long, companies_eligible_for_discount );