从JavaScript对象中的键中查找最高值

时间:2016-03-11 11:15:33

标签: javascript

我有以下对象:

var questions = [{
    'question_id': 245,
    'question': 'Question 1',
    'ordinal': 1,
    'master_difficulty': 1,
    'overridden_difficulty': ''
},{
    'question_id': 34,
    'question': 'Question 2',
    'ordinal': 2,
    'master_difficulty': 1,
    'overridden_difficulty': ''
},{
    'question_id': 656,
    'question': 'Question 3',
    'ordinal': 3,
    'master_difficulty': 1,
    'overridden_difficulty': ''
},{
    'question_id': 86,
    'question': 'Question 4',
    'ordinal': 4,
    'master_difficulty': 1,
    'overridden_difficulty': ''
}]

我希望从中得到最高的序数值。

e.g。 var highestOrdinal = highestOrdinalFunction(questions);

highestOrdinalFunction将返回最高序数。

所以在这种情况下,它会返回4

Math.max可以为此工作吗?

4 个答案:

答案 0 :(得分:4)

Array#reducespec | MDN)是这样做的惯用方法:

function highestOrdinalFunction() {
    return questions.reduce(function(previous, entry) {
        return previous === undefined || entry.ordinal > previous ? entry.ordinal : previous;
    }, undefined);
}

Array#reduce重复调用一个回调,传入回调的前一个返回值,该值以reduce的第二个参数播种。那件事通常被称为"累加器"用于减少操作。 reduce的结果是累加器的最终值。

如果undefined为空,则上述内容将返回questions

答案 1 :(得分:1)

我想这就是你要找的东西:

Math.max.apply(Math,questions.map(function(ques){return ques.ordinal;}))

有关map()如何运作的详情,请参阅Array.prototype.map

答案 2 :(得分:0)

我们可以通过ordinal值对该数组中的对象进行排序,然后返回第一个条目。

function highestOrdinalFunction( questions ) {
    return questions.sort( ( a, b ) => a.ordinal - b.ordinal )[ 0 ].ordinal;
}

<击>

在这个例子中,将Array.prototype.mapMath.max()结合起来可能更好一点:

Math.max.apply( null, questions.map(q => q.ordinal) );

答案 3 :(得分:0)

您可以通过两种方式找到最大值。您可以传递函数来映射属性的值,也可以直接在项目上调用属性。对于项目列表,这些结果将是要应用Math.max计算的映射值。

Math.prototype.max

&#13;
&#13;
var questions = [
  {'question_id':245,'question':'Question 1','ordinal':1,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':34,'question':'Question 2','ordinal':2,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':656,'question':'Question 3','ordinal':3,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':86,'question':'Question 4','ordinal':4,'master_difficulty':1,'overridden_difficulty':''}
]

document.body.innerHTML =  'Via Function: ' + highestOrdinalValueFn(questions) + '\n';
document.body.innerHTML += 'Via Property: ' + highestOrdinalValueProp(questions);

// #1 : Via function
function highestOrdinalValueFn(items) {
  return max(items, function() {
    return this.ordinal;
  });
}
// #2 : Via property
function highestOrdinalValueProp(items) {
  return max(items, 'ordinal');
}

function max(list, prop) {
  return Math.max.apply(Math, list.map(function(item) {
    return isFunction(prop) ? prop.call(item) : item[prop];
  }));
}

function isFunction(fn) {
  return fn && {}.toString.call(fn) === '[object Function]';
}
&#13;
body { white-space: pre; font-family: monospace; }
&#13;
&#13;
&#13;

Array.prototype.reduce

&#13;
&#13;
var questions = [
  {'question_id':245,'question':'Question 1','ordinal':1,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':34,'question':'Question 2','ordinal':2,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':656,'question':'Question 3','ordinal':3,'master_difficulty':1,'overridden_difficulty':''},
  {'question_id':86,'question':'Question 4','ordinal':4,'master_difficulty':1,'overridden_difficulty':''}
]

document.body.innerHTML = highestValue(questions, 'ordinal');

function highestValue(items, prop) {
  return compare(items, function(curr, prev) {
    return maxNumericComparator(curr, prev, prop);
  })[prop];
}

function maxNumericComparator(curr, prev, prop) {
  if (curr && !prev) return -1;
  if (!curr && prev) return 1;
  if (curr[prop] && !prev[prop]) return -1;
  if (!curr[prop] && prev[prop]) return 1;
  if (curr[prop] > prev[prop]) return -1;
  if (curr[prop] < prev[prop]) return 1;
  return 0;
};

function compare(list, comparator) {
  return list.reduce(function(max, item) {
    return comparator(item, max) < 0 ? item : max;
  }, null);
}
&#13;
&#13;
&#13;