在对象中查找给定整数的下一个属性

时间:2013-09-17 08:48:10

标签: javascript

我有一个对象

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
                }

此对象是标记列表。如果你获得900分,你就会得到1.0分。你得到8分,直到得到823分。在822点=>标记1.1

现在我有690分。我希望知道具有给定点的两个下一个对象属性。在这个例子中:较低的:678:1.9和较高的696:1.8。

除了我对该属性的完全打击,即570点我想要返回三个属性:较低,最高和完全命中。

我怎样才能获得这两个属性?

谢谢

修改

我对这个问题的解决方案是:


    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;
        }
    }

因为MrP和Get next key-value pair in an object也说根据ECMAScript的规范,对象是无序的,我决定创建一个数组并迭代抛出它们。

如果有人有更好的解决方案,这将是不错的

1 个答案:

答案 0 :(得分: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;
}

使用示例:

> 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}