在对象数组中查找相等的值

时间:2014-12-01 17:24:28

标签: javascript arrays angularjs object

    equal_points = {18:[
    {'scored_goals':500,'lost_goals': 520,'team':'team A'},
    {'scored_goals':490,'lost_goals': 530,'team':'team B'},
    {'scored_goals':500,'lost_goals': 510,'team':'team C'},
    {'scored_goals':490,'lost_goals': 500,'team':'team D'}
    ]};
var tmp_scored = [];
angular.forEach(equal_points, function(item) {
        angular.forEach(item, function(team) {
            tmp_scored.push(team.scored_goals);
            console.log(tmp_scored);
        });
});

嗨,我想按得分对象排序,但如果它们相等则按丢失的目标排序。如何检查哪些对象具有相同的值?

1 个答案:

答案 0 :(得分:2)

您可以使用数组的sort功能按scored_goalslost_goals进行排序,例如:

var arr = [
  {'scored_goals':500,'lost_goals': 520,'team':'team A'},
  {'scored_goals':490,'lost_goals': 530,'team':'team B'},
  {'scored_goals':500,'lost_goals': 510,'team':'team C'},
  {'scored_goals':490,'lost_goals': 500,'team':'team D'}
];

arr.sort(function (a,b) {
  return a.scored_goals>b.scored_goals && (a.lost_goals>b.lost_goals );
});

您可以在此处详细了解:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort