【问题标题】:Find equal values in array of objects在对象数组中查找相等的值
【发布时间】:2014-12-01 17:24:28
【问题描述】:
    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);
        });
});

您好,我想按得分对对象进行排序,但如果它们相等,则按失球对它们进行排序。如何检查哪些对象具有相同的值?

【问题讨论】:

    标签: javascript arrays angularjs object


    【解决方案1】:

    您可以使用数组的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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-18
      • 2017-12-02
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多