javascript - Comparing multiple values and assigning associated variables -
i have 4 variables need sort, , based on result, push associated values 1 of 2 arrays. values dynamic, , can match, need split in arrays (2 , 2), ideally , b going first array in event of tie.
var = 0; var b = 23.5; var c = 0; var d = 96.2; var ac, bc, cc, dc; var array1 = []; var array2 = []; //here im trying find top numbers, , based on value, push ac, bc, cc, or dc variables separate arrays. ((a > b && > c && > d) ? (array1.push(ac)): (array2.push(ac))); ((b > && b > c && b > d) ? (array1.push(bc)): (array2.push(bc))); ((c > && c > b && c > d) ? (array1.push(cc)): (array2.push(cc))); ((d > && d > c && d > d) ? (array1.push(dc)): (array2.push(dc))); obviously isnt working, finds highest number of four. there anyway without writing in every scenario?
edit: clarify, im trying compare first set (a, b, c, d) , based on outcome, sort second set (ac, bc, cc, dc) 2 arrays. method works comparing 2 variables (a > b) there 2 outcomes (true/false).
var = 0; var b = 23.5; var c = 0; var d = 96.2; var array1 = [ { priority: a, value: ac }, { priority: b, value: bc }, { priority: c, value: cc }, { priority: d, value: dc } ]; array1.sort(function(v1, v2) { return v1.priority - v2.priority; }); var array2 = array1.splice(2, 2); // remove 2 items starting @ index 2 now can refer values array1[0].value, etc.
edit: if want values, can this:
array2 = array2.map(function(v) { return v.value; });
Comments
Post a Comment