0

I have a large array of features with p and u properties. I want to find the smallest and highest p and u in the array and create this switch statement in a loop. This works about 99.9% of the time. However. I have one data set where the max and min turn out to be the same even if the values are evenly distributed and the average is not the same. Stumped.

            switch(true) {
                case p > max_p:
                    max_p = p;
                case u > max_u:
                    max_u = u;
                case p < min_p:
                    min_p = p;
                case u < min_u:
                    min_u = u;
            }

I run through the loop in firebug and can see that max_u gets sometimes updated if u < max_u. For example u = 0.066, max_u = 0.088.

Pycharm tells me about a fallthrough issue but the statement works fine on every other dataset I throw at it.

I can split the statement into two. The performance loss is minor but I would like to understand how this could happen.

Thanks, Dennis

edit:

Split into two statement that dataset works completely fine without a break in the statement.

            switch(true) {
                case p > max_p:
                    max_p = p;
                case p < min_p:
                    min_p = p;
            }
            switch(true) {
                case u > max_u:
                    max_u = u;
                case u < min_u:
                    min_u = u;
            }

edit: I accepted the answer given which works but I am still puzzled why something like this would happen.

enter image description here

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
  • 1
    For what it's worth, you could just use [`Math.max`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max). `max_u = Math.max(max_u, u)` – Mike Cluck Feb 19 '16 at 18:32
  • 3
    You should have `break;` after each `case` unless that was omitted. – Vinny M Feb 19 '16 at 18:32
  • @VinnyMannello but wouldn't the break be a problem since I am comparing 2 properties? Also the first property will be bigger than the smallest and smaller than the biggest and also might be either the biggest or smallest. – Dennis Bauszus Feb 19 '16 at 18:43
  • Can you share the dataset? At the top of my head can think of type conversion, addressing the same object, and switch optimization. –  Feb 19 '16 at 18:43
  • i cannot share the dataset easily i am afraid. i would like to use math.max(). problem is that the array is an array of objects from a big ajax/json response. this is happening in the success case. – Dennis Bauszus Feb 19 '16 at 18:52
  • 3
    What is the point of the switch(true)? I get trying new ways of doing things, but unless I'm missing something, that is making your code more complex than necessary – Hurricane Hamilton Feb 19 '16 at 19:41
  • i am not trying new ways but didn't know better. that's why i posted this here. thanks a lot to everyone who contributed. really helped me getting to know about Math.max / min. I wasn't aware of these functions before. – Dennis Bauszus Feb 19 '16 at 19:56

1 Answers1

3

Assuming you have an array of objects with u and p properties (if I've read the question correctly), here's a simple function that will give you the min/max values you want and save you the problems of using switch and/or if conditions.

var arr = [
 { p: 10, u: 101},
 { p: 11, u: 1},
 { p: 1, u: 3},
 { p: 2, u: 7},
 { p: 21, u: 1011},
 { p: 6, u: 2},
 { p: 2, u: 13}
]

function getInt(arr, key, type) {
  return Math[type].apply(null, arr.map(function (el) {
    return el[key];
  }));
}

var min_p = getInt(arr, 'p', 'min'); // 1
var max_p = getInt(arr, 'p', 'max'); // 21
var min_u = getInt(arr, 'u', 'min'); // 1
var max_u = getInt(arr, 'u', 'max'); // 1001

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • this works. i am still baffled why the single switch didn't work. – Dennis Bauszus Feb 19 '16 at 19:41
  • 1
    i had to iterate through the json anyways since i am creating geometry objects from each object in the array. in the end i just pushed a parseint and parsefloat into two arrays and then directly applied Math.max / min to the arrays to get the max and min values. Thanks for your help. – Dennis Bauszus Feb 19 '16 at 19:53