2017-12-15 12 views
0

다차원 배열 (플레이어와의 경기 목록)이 있으며 고유 한 플레이어 개체 만 가져 오기 위해이 배열을 필터링해야합니다.다차원 배열 필터 반응 찾기/찾기

다음 코드는 작동하지만 예상 한 결과가 다릅니다.

Const aPlayer=matchs 
    .filter(match => match.players.find(player => player.id  
===id)) 

변수 aPlayer에는이 플레이어의 모든 일치 항목이 포함되어 있습니다.

하지만 플레이어 개체 데이터 만 필요합니다. 그렇다면

Matches: List<Match> 
Match: { 
    ..., 
    players: List<Player> 
} 
Player: { 
    id: Index, 
    ... 
} 

, 당신이 이런 식으로 작업을 수행 할 수 있습니다 : 데이터는 다음과 같이 구성하면

답변

1

는 잘 모르겠어요

// still contains duplicates, but that's not the issue here 
const allPlayers = matchs 
    .map(x => x.players) 
    .reduce(x => (dest, val) => dest.concat(val), []); 

const player = allPlayers.find(x => x.id === id); 
+0

이 CONSOLE.LOG (allPLayers)를 작동하지 않습니다 = " – iCode

+0

정확히 작동하지 않으며 오류/결과는 무엇입니까? – Tobias

+0

Const allPlayers = 일치 .filter (match => match.players.find (player => player.id === id)) 다음은 const player = allPlayers.find (x => x.id === id); 일해라, 고마워. – iCode