简体   繁体   中英

Find equal values in array of objects

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

Hi,I would like sort object by scored but if they are equal then sort them by lost goals.How can I check which objects have the same values?

You can use the sort function of the Array to implement your sorting by scored_goals and also by lost_goals like:

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

You can read more about it here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM