从嵌套对象属性中获取除特定值之外的最高值

时间:2015-03-29 15:23:08

标签: javascript underscore.js

我在这里要做的是非常具体的,迭代逻辑超出了我的范围,我很害怕。好的......我有一个看起来像这样的对象:



var answers = {
  cat1: {
    q1: {
      "question": "Why?",
      "answer": "No",
      "points": 6
    },
    q2: {
      "question": "Why oh why?",
      "answer": "50%",
      "points": 15
    },
    q3: {
      "question": "Is it true?",
      "answer": "Maybe",
      "points": 9
    }
  },
  cat2: {
    q1: {
      "question": "Hello?",
      "answer": "Hello",
      "points": 0
    },
    q2: {
      "question": "Why me?",
      "answer": "Because",
      "points": 9
    },
    q3: {
      "question": "Is it true?",
      "answer": "Maybe",
      "points": 0
    }
  },
  cat3: {
    q1: {
      "question": "why not?",
      "answer": "I don't know",
      "points": 15
    },
    q2: {
      "question": "What?",
      "answer": "That",
      "points": 9
    },
    q3: {
      "question": "Is it real?",
      "answer": "Nope",
      "points": 6
    }
  }
}




对于每个"类别",我不知道的属性名称,我需要找到最高点,排除正好15个点(即15个是一个特殊情况,所以我会考虑9中最高的0,9和15)" q(n)"类别中的嵌套对象。

我想把这些高分并将它们加在一起,可能是通过+ =总共"变量

我觉得我应该以某种方式使用underscore.js简化对象,以便我只剩下q1: 6等,删除值为15的属性,然后使用下划线' s在将这些高分加在一起之前,_.max()函数会为每个q(n)取最高分值。

在示例中,这将是9加9加9(27)。

感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

您应该使用简单的for循环迭代Object的键,然后您可以总结结果。

var maxs = {},
      tot = 0;

for (var i in answers) {
    maxs[i] = 0;
    for (var j in answers[i]) {
        if (answers[i][j].points != 15) maxs[i] = Math.max(maxs[i], answers[i][j].points);
        else delete answers[i][j];
        // ^ delete the question if the score is 15
    }
    tot += maxs[i];
}

结果将是这样的:

maxs
> {
   cat1: 9,
   cat2: 9,
   cat3: 9
}

tot
> 27

答案 1 :(得分:1)

您可以将_.mapObject_.max的自定义迭代项一起使用,以便按类别获得最高分:

var maxes = _.mapObject(answers, function (qs) {
    var maxq = _.max(qs, function (q) {
        return (q.points !== 15) ? q.points : -Infinity;
    });

    return maxq.points;
});

将输出

  

{cat1 = 9,cat2 = 9,cat3 = 9}

http://jsfiddle.net/v80z9w2y/演示

_.reduce会让你得到总数:

var sum = _.reduce(maxes, function(memo, num){ return memo + num; }, 0);

http://jsfiddle.net/v80z9w2y/1/

如果您只对这笔钱感兴趣,那么您当然可以将这两个步骤结合起来:

var sum = _.reduce(answers, function (memo, qs) {
    var maxq = _.max(qs, function(q) {
        return (q.points !== 15) ? q.points : -Infinity;
    });

    return memo + maxq.points;
}, 0);

http://jsfiddle.net/v80z9w2y/2/

答案 2 :(得分:1)

这是另一个使用下划线的版本:

    var sum = _.reduce(answers, function(memo, cat){
        return memo + _.max(_.without( _.pluck(cat, 'points'), 15) );
    }, 0);

答案 3 :(得分:0)

此代码适用于任何现代浏览器,无需任何库

var categories = [];
for (var cat in answers) {
  categories.push(answers[cat]);
}
var sum = categories.map(function (cat) {
  var highest = 0;
  for (var q in cat) {
    var question = cat[q];
    if (question.points !== 15) {
      highest = Math.max(highest, question.points);
    }
  }
  return highest;
}).reduce(function (prev, current) {
  return prev + current;
}, 0);

答案 4 :(得分:0)

已经选择了答案,但问题很有趣,所以这是我的尝试:

function getTotal(answers){
    return Object.keys(answers).reduce(function(total, cat){
        return total + Math.max.apply(window, 
            Object.keys(answers[cat]).map(function(question){
                return answers[cat][question].points === 15 ? 0 : answers[cat][question].points;
            })
        );
    },0);
}

Demo on JSFiddle