I provided a snippet that you can run in the console to test the functionality, and created a demo function that you could use right away (deleting console.log statements). It returns an array of company names.
In fact, what I'm doing is using the fact that Javascript has its own associative arrays for objects, so I assign the name toLowerCase
field (in your case the company) as the field for the search point of the associative array. If the field name is no longer a property, then this is the first time we have added it. The first time we add one (consider "bobo"), we will set it to zero. In subsequent times, we increase 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 );
source share