所以我有一个API,可以为我提供足球比赛结果:
results:[
{
Group:"A"
Home: "Fc Barcelona",
Away: "Fc Porto"
Score: {
Home: 0,
Away: 0
},
{
Group:"A"
Home: "AC Milan",
Away: "Fc Barcelona"
Score: {
Home: 0,
Away: 1
},
{
Group:"A"
Home: "FC Barcelona",
Away: "AC Milan"
Score: {
Home: 2,
Away: 0
}
{
Group:"B"
Home: "Juventus",
Away: "Real Madrid"
Score: {
Home: 0,
Away: 1
}
]
等...
此外,我想基于组对常规表进行排序,但是我有团队重复项,而且我无法根据得分从多个游戏中总结出一个团队的得分
到目前为止,我一直在尝试使用if语句(如我提供的代码中的代码)进行整理,如果有更好的解决方案,请告诉我!
<tbody v-for="result in results" :key="result.index">
<div v-if="result.Group=='A'>
<tr>
<td>{{result.Home}}</td> <br> <-- i need teams to be print out once, not several times
<td>{{result.Score.Home}}</td> <-- i need this to be 'if result.Score.Home>0 add 3 points to the adequate team and print it out here'
</tr>
</div>
</tbody>
data () {
return {
results:[]
}
mounted () {
axios
.get('http://localhost/soccer/results',)
.then(response => (this.results = response.data))
}
使用示例中的代码,我将得到以下结果:
Team Score PTS
Fc Barcelona 0
Ac Milan 0
Fc Barcelona 2
我需要实现这一目标(不需要得分,需要根据得分来计算PTS,并且只需要像这样打印一次团队):
Team Score PTS
Fc Barcelona N/A 3
Ac Milan N/A 0
答案 0 :(得分:0)
不要像处理传入的数据那样,而是以可以方便使用的任何格式来构成模板的模板,然后尝试编写一个函数以将数据转换为该格式。
在这种情况下,我想遍历各组,并让每个组包含其团队。我把嵌套的东西缠起来了。
const axiosPromise = Promise.resolve([{
Group: "A",
Home: "Fc Barcelona",
Away: "Fc Porto",
Score: {
Home: 0,
Away: 0
}
},
{
Group: "A",
Home: "AC Milan",
Away: "Fc Barcelona",
Score: {
Home: 0,
Away: 1
}
},
{
Group: "A",
Home: "FC Barcelona",
Away: "AC Milan",
Score: {
Home: 2,
Away: 0
}
},
{
Group: "B",
Home: "Juventus",
Away: "Real Madrid",
Score: {
Home: 0,
Away: 1
}
}
]);
new Vue({
el: '#app',
data: {
groups: {}
},
mounted() {
axiosPromise.then((results) => this.transformResults(results));
},
methods: {
transformResults(axiosResults) {
const result = {};
axiosResults.forEach((obj) => {
if (!(obj.Group in result)) {
result[obj.Group] = {};
}
const g = result[obj.Group];
if (!(obj.Home in g)) {
g[obj.Home] = 0;
}
if (!(obj.Away in g)) {
g[obj.Away] = 0;
}
g[obj.Home] += obj.Score.Home;
g[obj.Away] += obj.Score.Away;
});
this.groups = result;
}
}
});
table {
border-collapse: collapse;
margin: 1rem 0;
outline: thin solid black;
}
th,
td {
border: thin solid gray;
padding: 0 0.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="(g, name) in groups">
Group: {{name}}
<table>
<tr>
<th>Team</th>
<th>Points</th>
</tr>
<tr v-for="(points, name) in g">
<template v-if="points > 0">
<td>{{name}}</td>
<td>{{points}}</td>
</template>
</tr>
</table>
</div>
</div>
答案 1 :(得分:0)
以下是解决您的问题的热门示例(使用console.log)。您的数据包含同一俱乐部(巴塞罗那足球俱乐部,巴塞罗那足球俱乐部)的不同名称。使用toLowerCase / toUpperCase或其他函数来规范您的数据。
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
const results = [
{
Group:"A",
Home: "FC Barcelona",
Away: "FC Porto",
Score: {
Home: 0,
Away: 0
},
},
{
Group:"A",
Home: "AC Milan",
Away: "FC Barcelona",
Score: {
Home: 0,
Away: 1
},
},
{
Group:"A",
Home: "FC Barcelona",
Away: "AC Milan",
Score: {
Home: 2,
Away: 0
},
},
{
Group:"B",
Home: "Juventus",
Away: "Real Madrid",
Score: {
Home: 0,
Away: 1
}
}
]
const groupGames = groupBy(results, 'Group')
const m = new Map()
for(let key in groupGames) {
const group = groupGames[key]
const results = new Map()
group.forEach(game => {
if (!results.has(game.Home)) {
results.set(game.Home, { pts: 0 })
}
let home = results.get(game.Home)
if (!results.has(game.Away)) {
results.set(game.Away, { pts: 0 })
}
let away = results.get(game.Away)
if (game.Score.Home === game.Score.Away) {
home.pts += 1
away.pts += 1
}
if (game.Score.Home > game.Score.Away) {
home.pts += 3
}
if (game.Score.Home < game.Score.Away) {
away.pts += 3
}
})
m.set(key, results)
}
m.forEach((value, key) => {
console.log('Group:', key)
value.forEach((v, k) => {
console.log('Club:', k, 'Points:', v.pts)
})
})
并输出
Group: A
Club: FC Barcelona Points: 7
Club: FC Porto Points: 1
Club: AC Milan Points: 0
Group: B
Club: Juventus Points: 0
Club: Real Madrid Points: 3