0

i have an Object

marks: {
                    900: 1.0,
                    822: 1.1,
                    804: 1.2,
                    786: 1.3,
                    768: 1.4,
                    750: 1.5,
                    732: 1.6,
                    714: 1.7,
                    696: 1.8,
                    678: 1.9,
                    660: 2.0,
                    588: 2.4,
                    570: 2.5,
                    552: 2.6,
                    534: 2.7,
                    516: 2.8,
                    498: 2.9,
                    480: 3.0,
                    462: 3.1,
                    444: 3.2,
                    426: 3.3,
                    408: 3.4,
                    390: 3.5,
                    372: 3.6,
                    354: 3.7,
                    336: 3.8,
                    318: 3.9,
                    300: 4.0
                }

This object is a mark-list. if you get 900 points you get the mark 1.0. you get the mark 1 until you get 823 points. on 822 points => mark 1.1

now i have 690 points. i want the know the both next object properties with the given points. in this example: the lower: 678: 1.9 and the upper 696: 1.8.

except i get a full hit on the propertie i.e. 570 points i want have three properties returned: the lower, the upper and the full hit.

how can i get these both properties?

Thank you

EDIT:

My solution for that problem is:


    var a = new Array(),
        r = new Array(),
        points = 300;

    for(var key in this.options.marks){
        a.push({'points':key, 'mark': this.options.marks[key]});
    }
    a.reverse();

    for (var i=0; i  a[i].points) {

            r.push({'points':a[(i-1)].points, 'mark': a[(i-1)].mark});
            r.push({'points':points, 'mark': a[(i-1)].mark});
            r.push({'points':a[i].points, 'mark': a[i].mark});

            break;
        }else if(points == a[i].points) {

            if(typeof a[i-1] != 'undefined'){
                r.push({'points':a[i-1].points, 'mark': a[i-1].mark});
            }else{
                r.push({'points':a[i].points, 'mark': a[i].mark});
            }

            r.push({'points':a[i].points, 'mark': a[i].mark});

            if(typeof a[i+1] != 'undefined'){
                r.push({'points':a[i+1].points, 'mark': a[i+1].mark});
            }else{
                r.push({'points':a[i].points, 'mark': a[i].mark});
            }

            break;
        }
    }

as MrP and Get next key-value pair in an object also said that objects are unordered as per ECMAScript's spec, i decided to make an array and iterate throw them.

if anyone has a better solution for this would be nice

Community
  • 1
  • 1
JuKe
  • 663
  • 2
  • 7
  • 20
  • 2
    If it was me building this system, I wouldn't be using an object, but I would use an array of objects, sorted on the mark. That way you can loop over the array until you find the mark you need. It will be harder when you have an object since you don't have an order and will need to loop over the entire object. – MrP Sep 17 '13 at 08:50
  • @CBroe: i have tried somthing like this... http://jsfiddle.net/Ww3cw/2/ – JuKe Sep 17 '13 at 09:07

1 Answers1

0
function getClosestScores(points) {
  var marks = { 100: 4.4, 200: 5.5, 300: 8.8 };
  // ^ Replace this with your source data ^

  var marksArray = [];
  for (var p in marks)
    if (marks.hasOwnProperty(p))
      marksArray.push({points: p, score: marks[p]});
  marksArray.sort(function(a, b) { return a.points - b.points; });

  result = { lower: null, full: null, upper: null };
  for (var i=0; i < marksArray.length; i++) {
    var it = marksArray[i];
    if (it.points < points)
      result.lower = it;
    if (it.points == points)
      result.full = it;
    if (it.points > points) {
      result.upper = it;
      break;
    }
  }

  return result;
}

Example use:

> console.log(JSON.stringify(getClosestScores(100)));
{"lower":null,"full":{"points":"100","score":4.4},"upper":{"points":"200","score":5.5}}
> console.log(JSON.stringify(getClosestScores(150)));
{"lower":{"points":"100","score":4.4},"full":null,"upper":{"points":"200","score":5.5}}
> console.log(JSON.stringify(getClosestScores(500)));
{"lower":{"points":"300","score":8.8},"full":null,"upper":null}
Deestan
  • 16,738
  • 4
  • 32
  • 48