4

I have an array with registrants from multiple companies (some from the same companies, some not) and I need to count how many people from the same company have registered. So I need a number that tells me how many extra people (after the first) from unique companies have registered.

Say I have an array:

var company_names = ['acme', 'acme', 'bobo', 'comanche', 'acme', 'comanche'];

and a variable:

var companies_eligible_for_discount = 0;

How would I count that 3 discounts are to be assigned? (2 for "acme" and 1 for "comanche")

Jiminy Cricket
  • 908
  • 1
  • 9
  • 24
  • Ive been trying just about everything. So much to the point where I feel like starting from square 1. – Jiminy Cricket Nov 26 '11 at 03:56
  • let me know if my answer isn't quite understandable. I know that I added a bit of meat to it, hoping to keep it fleshed out for you. RightSaidFred gave the more compact version, it just depends on what you're looking to start and end with. – jcolebrand Nov 26 '11 at 04:07

2 Answers2

5
var dupes = {};

company_names.forEach(function(v,i) {
    if( v in dupes ) {
        dupes[v]++;
        companies_eligible_for_discount++;
    }
    else dupes[v] = 0;
});

var dupes = {}, v, i;

for( i = 0; i < company_names.length; ++i ) {
    v = company_names[i];
    if( v in dupes ) {
        dupes[v]++;
        companies_eligible_for_discount++;
    }
    else dupes[v] = 0;
}
RightSaidFred
  • 11,209
  • 35
  • 35
0

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 );
jcolebrand
  • 15,889
  • 12
  • 75
  • 121