I have jQuery included so if it helps to use it, it’s available.
First the simple question:
Is there a way to check if several vars are all equal to each other?
I can use the transitive relation logic and do
if ((a == b) && (b == c)) && (c == d)) ... {
to avoid checking every variable against EACH other, but I think there should be a fancier way to do this.
If you can answer this first part only, it would be much appreciated.
Now, the tricky part…
I have a variable amount of variables (between 1 and 5)
I know that their value can be any of 200 possible values from a DDBB.
What would be the best way to know how many instances of each value I have within those variables?
For example…
If I have…
var1 = VALUE_A;
var2 = VALUE_A;
var3 = VALUE_B;
var4 = VALUE_Z;
var5 = VALUE_Z;
… i want to get something like:
result["VALUE_A"] => 2
result["VALUE_B"] => 1
result["VALUE_Z"] => 2
///////////////////////////
OR if i have…
var1 = VALUE_A;
var2 = VALUE_C;
var3 = VALUE_B;
… get:
result["VALUE_A"] => 1
result["VALUE_C"] => 1
result["VALUE_B"] => 1
///////////////////////////
OR if i have…
var1 = VALUE_A;
var2 = VALUE_A;
var3 = VALUE_A;
var4 = VALUE_C;
var5 = VALUE_C;
… get:
result["VALUE_A"] => 3
result["VALUE_C"] => 2
///////////////////////////
OR if i have…
var1 = VALUE_A;
var2 = VALUE_A;
var3 = VALUE_A;
… get:
result["VALUE_A"] => 3
Hope I was clear. Examples were the only way I could think of explaining clearly.
If this is too complex for Javascript or processing so many possible values up to times 5 can make the browser slow I can do it in PHP and get the result via AJAX but I’d rather not.
Would something like this do?
This function accepts an array of the values, and returns an object whose keys are the elements and values are the number of occurrences of each.