2017-09-22 6 views
0

私は〜1800の配列を持っています。オブジェクトはリーグで行われたゲームを表しています。各チームのオブジェクトを持つ新しい配列が必要で、4つの新しいフィールド(wins,lossesties、およびpoints)が追加されます。javascriptでマップを作成して同時に配列を減らす

[ 
    { 
    "homeGoals": 2, 
    "gameId": "12221", 
    "homeTeam": { 
     "id": "aasfdsf1", 
     "teamName": "Team 1" 
    }, 
    "awayTeam": { 
     "id": "aasfdsf2", 
     "teamName": "Team 2" 
    }, 
    "id": "ggaew1", 
    "awayGoals": 4 
    }, 
    { 
    "homeGoals": 5, 
    "gameId": "12222", 
    "homeTeam": { 
     "id": "aasfdsf1", 
     "teamName": "Team 1" 
    }, 
    "awayTeam": { 
     "id": "aasfdsf3", 
     "teamName": "Team 3" 
    }, 
    "id": "ggaew2", 
    "awayGoals": 1 
    }, 
    { 
    "homeGoals": 4, 
    "gameId": "12223", 
    "homeTeam": { 
     "id": "aasfdsf2", 
     "teamName": "Team 2" 
    }, 
    "awayTeam": { 
     "id": "aasfdsf3", 
     "teamName": "Team 3" 
    }, 
    "id": "ggaew3", 
    "awayGoals": 4 
    }, 
    { 
    "homeGoals": null, 
    "gameId": "12223", 
    "homeTeam": { 
     "id": "aasfdsf2", 
     "teamName": "Team 2" 
    }, 
    "awayTeam": { 
     "id": "aasfdsf3", 
     "teamName": "Team 3" 
    }, 
    "id": "ggaew4", 
    "awayGoals": null 
    } 
] 

そして、ここでは、私は結果がどのように見えるために必要なものの一例である:ここでは私が働いている配列のサンプルですいくつかのゲームがプレイされていない

[ 
    { 
    "id": "aasfdsf1", 
    "name": "Team 1", 
    "wins": 1, 
    "losses": 1, 
    "ties": 0, 
    "points": 2 
    }, 
    { 
    "id": "aasfdsf2", 
    "name": "Team 2", 
    "wins": 1, 
    "losses": 0, 
    "ties": 1, 
    "points": 3 
    }, 
    { 
    "id": "aasfdsf3", 
    "name": "Team 3", 
    "wins": 0, 
    "losses": 1, 
    "ties": 1, 
    "points": 1 
    } 
] 

ので、 homeGoalsおよびawayGoalsフィールドはnullになります。

は、これまでのところ私は、ゲームが終了している場合にのみ、ユニークなチームのリストを持っている:

​​

私は機能を減らすのいくつかの並べ替えを行う必要がありますが、トラブルを考え出すを持っています知っています。私は、適切なマップリダクション機能があれば、前にやったステップが無関係であると確信しています。どんな助けでも大歓迎です!

答えて

1

私はあなたがこのような何かを探していると思う:

const hashMapTeams = games.filter(x => x.homeGoals !== null) 
.reduce((res, match)=>{ 
    /* do the calculations here */ 
    /* put the values on the res object, using res as a HashMap*/ 
    res["/*the home team id*/"].id = /*id value*/ 
    res["/*the home team id*/"].name = /*name value*/ 
    res["/*the home team id*/"].wins= /* the right value */; 
    res["/*the home team id*/"].losses= /* the right value */; 
    res["/*the home team id*/"].ties= /* the right value */; 
    res["/*the home team id*/"].points= /* the right value */; 

    res["/*the away team id*/"].id = /*id value*/ 
    res["/*the away team id*/"].name = /*name value*/ 
    res["/*the away team id*/"].wins= /* the right value */; 
    res["/*the away team id*/"].losses= /* the right value */; 
    res["/*the away team id*/"].ties= /* the right value */; 
    res["/*the away team id*/"].points= /* the right value */; 
},{}); 

/* This will convert again the object to an array */ 
const arrayTeams = Object.keys(hashMapTeams).map(function (key) { return hashMapTeams[key]; }); 
0

これは、あなたが探している正確に結果を取得:私はあなたに複数の方法を示すことtenaryとブラケットを使用

{ 
    "id": "aasfdsf1", 
    "name": "Team 1", 
    "wins": 1, 
    "losses": 1, 
    "ties": 0, 
    "points": 2 
}, 

それに近づくには、どちらかを使うことができます。

let result = []; 

your1800ArrayObj.map(data => {  
    let wins = data.wins ? data.wins : 0; 
    let losses = data.losses ? data.losses : 0; 
    let ties = data['ties'] || 0; 
    let points = data['points'] || 0; 

    if (data.homeGoals === null && data.awayGoals === null) { 
     console.log('game not played') 
     } else { 
      if (data.homeGoals > data.awayGoals) { 
       wins += 1 
       points += 1 
      } else if (data.homeGoals < data.awayGoals) { 
       losses += 1 
      } else { 
       ties += 1 
      } 
     } 

    result.push({ 
     id: data.id, 
     name: data.homeTeam.teamName , 
     wins: wins, 
     losses: losses, 
     ties: ties, 
     points: points 
    }) 
}) 

return result 
} 
関連する問題