2 개의 배열 (팀 이름 목록)을 포함하는 데이터 개체가 있습니다.
const data = {
Liga: ['Alaves','Atletico', 'Barcelona', 'Betis'],
Premier: ["Arsenal", "Chelsea", "Everton", 'Liverpool']
};
내가 선택한 값 (Liga 또는 Premier)에 따라 팀 목록을 얻습니다.
이 팀에는 두 가지 데이터 점수 결과가 scoresliga
있으며scorespremier
const scoresliga = [
{ day: '0', Alaves: -6, Atletico: -1, Barcelona: -2, Betis: -6},
{ day: '1', Alaves: -7, Atletico: 1, Barcelona: -2, Betis: -6}
];
const scorespremier = [
{ day: '0', Arsenal: -6, Chelsea: -1, Everton: -2, Liverpool: -6},
{ day: '1', Arsenal: -7, Chelsea: 1, Everton: -2, Betis: -6}
];
내 문제는 내가 'Liga'를 선택하면 scoresliga
개체 를 소비해야하고 'Premier'를 선택하면 소비해야하는 방법을 찾아야한다는 것입니다.scorespremier
이것은 또한 현재 일반적인 기능 이 아닌 팀의 평균을 계산할 때 내가 가진 문제를 해결하는 것입니다 .
이것은 scoresliga
데이터 객체를 사용하는 함수의 스 니펫 이지만 Ligas (Premier 및 Liga) 모두에 대한 공유 함수 여야합니다.
const getAverage = team => {
if (isNaN(scoresliga[0][team])) return null;
return scoresliga.map(x => x[team]).reduce((a, c) => a + c) / scoresliga.length;
};
나는 내 사건을 재현 한 모습이 여기에
getAverage에 추가 속성 만 추가하면됩니다.
const getAverage = (league, team) => {
if (isNaN(league[0][team])) return null;
return league.map(x => x[team]).reduce((a, c) => a + c) /
league.length;
};
또한 점수에 매핑을 만듭니다.
const scores = {
Liga: [
{ day: "0", Alaves: -6, Atletico: -1, Barcelona: -2, Betis: -6 },
{ day: "1", Alaves: -7, Atletico: 1, Barcelona: -2, Betis: -6 }
],
Premier: [
{ day: "0", Arsenal: -6, Chelsea: -1, Everton: -2, Liverpool: -6 },
{ day: "1", Arsenal: -7, Chelsea: 1, Everton: -2, Betis: -6 }
]
};
그리고 Chart.js에서
<span>{getAverage(scores[this.state.selectedLeague], this.state.selectedTeam)}</span>
또한 업데이트
handleLeagueChange = event => {
const selectedLeague = event.target.value;
this.setState({ selectedLeague: selectedLeague }, () => {
this.setState({ selectedTeam: data[selectedLeague][0] });
});
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다