3

I'd like to check for a variable within my array. It works if the number matches my value directly. How can I get it to match the previous value if it's below the match. From my code :

var assassin_bp = {
    fhr: { 7:8, 15:7, 27:6, 48:5, 86:4, 200:3 },
    fcr: { 8:15, 16:14, 27:13, 42:12, 65:11, 102:10, 174:9 }
}
var char_fhr = 48;
var fhr_frames = assassin_bp[ 'fhr' ][ [char_fhr] ]

with char_fhr worth 48, fhr_frames returns 5,
if char_fhr was worth 47 (or any number from 27 to 47), fhr_frames should return 6,
if char_fhr was worth 49 (or any number from 48 to 85), fhr_frames should return 5

I'm clueless on where to start. Can an object include a range? Should I have a function checking for it?

4
  • 1
    Is there any predictability to the ranges? Your current ranges seem random. Is that they way they actually are? Oct 9, 2012 at 18:02
  • ...fyi, you have an extra set of [] around the char_fhr variable. Harmless in this exact situation, but should probably be removed. Oct 9, 2012 at 18:04
  • Why can't you just use a for-loop to go through the array?
    – KLee1
    Oct 9, 2012 at 18:05
  • the ranges are defined by the previous (or current) data set. they will not change once set in the object.
    – veksen
    Oct 9, 2012 at 18:08

2 Answers 2

2

You need to iterate over the object properties and compare their names against your target value. See for...in [MDN] and Working with Objects [MDN].

function valueForClosestKey(obj, target) {
    // get all keys smaller than or equal to `target`
    var keys = [];
    for(var k in obj) {
        if(k <= target) {
            keys.push(k);
        }
    }

    // order keys in ascending order
    keys.sort(function(a, b) {
        return a - b;
    }); 
    // e.g. `keys` is now [7, 15, 27, 48]      

    // get the "closest" key, which is the last one in the array
    if(keys.length > 0) {
        return obj[keys.pop()];
    }
    // if there are no keys smaller than `target` (i.e. `keys` is empty),
    // we just don't return anything (implicitly returns `undefined`)
}

var value = valueForClosestKey(assassin_bp[ 'fhr' ], char_fhr);

It is necessary to extract and sort the keys this way because the order in which an object is iterated is implementation dependent. I.e. it is not guaranteed that the properties are in the correct numerical order.

15
  • I don't think you need the Array conversion and the sort. The order is constrained by your ultimate for statement, which enforces the numeric sequence. Or am I missing something. Oct 9, 2012 at 18:12
  • Changed my code to make it simpler, but in any case extracting the keys is required if I want to iterate over the keys in their natural order. The order of iterating over objects with for...in is not defined. Oct 9, 2012 at 18:16
  • But there is no natural order for an object. The only order we can get is that which we enforce when enumerating. It happens that in the question, the keys appear in numeric order, but if they didn't, there would be no way to enforce it the original definition. Oct 9, 2012 at 18:18
  • Right... I assume the keys are supposed to be in numeric order. That's why I extract them and sort them. Otherwise I could not be certain I get the keys in that order. If the OP wants the keys in the order they have been defined... well, that's not possible. Oct 9, 2012 at 18:19
  • I guess I'm assuming we know where the floor is. If that can be known, we can just iterate the original object in reverse as though it was an Array. If we can't assume a given floor, then I could see that we would need to grab and sort the keys. ...Side note, you can just do return obj[keys.pop()];, since the default return value is undefined, but being explicit is good too. Oct 9, 2012 at 18:23
1

http://jsfiddle.net/aquinas/QP6aF/

How about:

var assassin_bp = {
    fhr: { 7:8, 15:7, 27:6, 48:5, 86:4, 200:3 },
    fcr: { 8:15, 16:14, 27:13, 42:12, 65:11, 102:10, 174:9 }
}

var arr = [];

for (var prop in assassin_bp.fhr){
    if (assassin_bp.fhr.hasOwnProperty(prop)){
        arr.push(parseInt(prop));
    }
}

arr.sort(function(a,b){return a-b;});

function calcScore(val){
    if (arr[0] > val){
        return assassin_bp.fhr[arr[0]];
    }

    for (var i=arr.length; i>=0; i--){
        if (val >= arr[i]){
            return assassin_bp.fhr[arr[i]];
        }
    }
}

//test to see if this works:
for (var i=0; i<300; i++){
    document.write(i + " - " + calcScore(i) + "<br/>");
}                    
        ​
1
  • Thanks! this works for me, however I'm trying to merge the function as seen jsfiddle.net/veksen/JtrLM I feel like I'm not too far :/
    – veksen
    Oct 9, 2012 at 19:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.