import {Player} from './player';
export const PlayersData : Player[] = [
{id:1,name:"Marc-andré Ter stegen",number:"1",post:"Goalkeeper",goals:0,assist:0,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/150720.jpg"},
{id:2,name:"Gerrad Piqué",number:"3",post:"Defender",goals:0,assist:0,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/27798.jpg"},
{id:3,name:"Ivan Rakitić",number:"4",post:"Midfielder",goals:0,assist:0,likes:7,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/35308.jpg"},
{id:4,name:"Sergie Buskquets",number:"5",post:"Midfielder",goals:0,assist:0,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/124973.jpg"},
{id:5,name:"Denis Suarez",number:"6",post:"Midfielder",goals:20,assist:20,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/200516.jpg"},
{id:6,name:"Arda Turan",number:"7",post:"Midfielder",goals:0,assist:10,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/30203.jpg"},
{id:7,name:"Andres Iniesta",number:"8",post:"Midfielder",goals:10,assist:20,likes:0,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/5057.jpg"},
{id:8,name:"Luis Suarez",number:"9",post:"Forward",goals:0,assist:0,likes:30,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/43635.jpg"},
{id:9,name:"Lionel Messi",number:"10",post:"Forward",goals:0,assist:0,likes:40,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/26622.jpg"},
{id:10,name:"Neymar Jr",number:"11",post:"Forward",goals:30,assist:10,likes:30,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/142105.jpg"},
{id:11,name:"Javier Mascherano",number:"14",post:"Defender",goals:0,assist:0,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/22185.jpg"},
{id:12,name:"Jordi Alba",number:"18",post:"Defender",goals:0,assist:0,likes:10,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/125417.jpg"},
{id:13,name:"Sergio Roberto",number:"20",post:"Midfielder",goals:0,assist:0,likes:20,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/171905.jpg"},
{id:14,name:"Samuel Umtiti",number:"23",post:"Defender",goals:0,assist:0,likes:9,dislike:0,img:"http://s.weltsport.net/bilder/spieler/gross/170711.jpg"}
];
这是我在打字稿中试过的
export class DashboardComponent implements OnInit {
players : Player[] = [];
bestPlayer:Player;
constructor(private playerService : PlayerService) { }
max =0;
bestPlayer = this.players[0];
ngOnInit() {
this.playerService.getPlayers()
.then(players=> this.players = players);
for (player of players)
if (player.likes>max) {
max => player.likes;
bestPlayer => player;
}
}
}
答案 0 :(得分:0)
正如伊戈尔所说,你应该首先阅读重复的主题。 我还建议你的其他卡住,get对象包含最大值。 请阅读其他主题以获得明确答案Finding the max value of an attribute in an array of objects
export class DashboardComponent implements OnInit {
players : Player[] = [];
bestPlayer:Player;
constructor(private playerService : PlayerService) { }
max =0;
ngOnInit() {
this.playerService.getPlayers()
.then(players=> {
this.players = players;
let maxValue = Math.max.apply(Math,players.map(function(o){return o.likes;}));
this.bestPlayer = players.filter(function(o) { return o.likes === maxValue; })[0];
});
}
}
答案 1 :(得分:0)
我建议使用Lodash。他们有一个maxBy function来解析一个对象数组,并按属性返回一个具有最大值的对象。
来自他们网站的示例。
var objects = [{ 'n': 1 }, { 'n': 2 }];
_.maxBy(objects, function(o) { return o.n; });
// => { 'n': 2 }
// The `_.property` iteratee shorthand.
_.maxBy(objects, 'n');
// => { 'n': 2 }
答案 2 :(得分:0)
我找到了解决方案:
bestPlayer:Player;
data:Player[];
max:number =0;
constructor(private playerService : PlayerService) { }
ngOnInit() {
this.data=this.playerService.getDatas();
for (var i = 0; i <= this.data.length; i++) {
if (this.data[i].likes>this.max) {
this.max=this.data[i].likes;
this.bestPlayer=this.data[i];
}
}