Как объединить 2 строки данных в одну строку? - PullRequest
1 голос
/ 26 июня 2019

Я пытаюсь объединить 2 Line данных в одну строку для каждой строки. Например 1st row, который Green Bay Packers @ Chicago Bears строка должна содержать +3.5000 and -3.5000 данные, аналогично для Atlanta @ Minnesota строка должна содержать +46.000 and +46.000. Как мне добиться такого выхода?

x.component.ts code

import {Component} from '@angular/core';
import {HttpClient} from '@angular/common/http'
import {forkJoin} from 'rxjs';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-mlb-api',
  templateUrl: './mlb-api.component.html',
  styleUrls: ['./mlb-api.component.css']
})
export class MlbApiComponent  {
//allhomeTeamName;
allawayTeamName;
allline;
allName;
all: Array<{line: string, name: string}> = [];
firstLinePerGame: Array<string>;
oddsAmericans: Array<string>;

  constructor(private http: HttpClient) {}

  ngOnInit() {

    const character = this.http.get('https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json').pipe(map((re: any) => re.events));
    const characterHomeworld = this.http.get('https://www.fantasylabs.com/api/sportevents/3/2019_06_17');

    this.firstLinePerGame = new Array<string>();
    this.oddsAmericans = new Array<string>();

    forkJoin([character, characterHomeworld]).subscribe(([draftkingsResp, fantasylabsResp]) => {      
        this.allName = draftkingsResp.map(r => r.name);
      //this.allhomeTeamName = draftkingsResp.map(r => r.name);
      this.allawayTeamName = draftkingsResp.map(r => r.awayTeamName);
      this.allline = draftkingsResp.map(r=>r.offers).flat().map(r => r.outcomes).flat().map(o => o.line);
      this.allline = this.allline.filter(l => !!l);
      this.createAllArray();      

      draftkingsResp.forEach(r => {

        if(r.offers && r.offers.length) {

          if(r.offers.length === 3) {
            const firstGame = r.offers[0].outcomes[0];
            this.firstLinePerGame.push(firstGame.line);

            // const secondGame = r.offers[2].outcomes[0];
            // this.firstLinePerGame.push(secondGame.line);

            this.oddsAmericans.push(r.offers[1].outcomes[0].oddsAmerican)
            this.oddsAmericans.push(r.offers[1].outcomes[1].oddsAmerican)

          } else if(r.offers.length === 1) {

            const firstGame = r.offers[0].outcomes[0];
            this.firstLinePerGame.push(firstGame.line);

          } else if(r.offers.length === 2) {
            console.log('******************')
          }
        }
      })      

      console.log(this.firstLinePerGame.filter(l => l));
      console.log(this.oddsAmericans);
    });
  }

  createAllArray(): void {
    for (let i = 0; i < this.allline.length; i++) {
      let item = {
        line: this.allline[i],
        name:this.allName[i],
        oddsAmericans:this.oddsAmericans[i]
        //awayTeam: this.allawayTeamName[i],
        //homeTeam: this.allhomeTeamName[i]
      }
      this.all.push(item);
    }
  }
}

y.component.html code

<table class="table table-striped table-condensed table-hover">
  <thead>
      <tr>

          <th class="awayTeamName">Name&nbsp;<a ng-click="sort_by('Name')"><i class="icon-sort"></i></a></th>

           <th class="line">Line&nbsp;<a ng-click="sort_by('line')"><i class="icon-sort"></i></a></th>

           <th class="line">Money Line&nbsp;<a ng-click="sort_by('Money')"><i class="icon-sort"></i></a></th>
      </tr>
  </thead>

  <tbody>
    <ng-container *ngFor="let item of all | paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
      <tr>
        <td>{{item.name }}</td>

        <td>{{item.line}}</td>
        <td>{{item.oddsAmericans}}</td>

      </tr>
    </ng-container>
  </tbody>
</table> 

<pagination-controls (pageChange)="p = $event"></pagination-controls>

Вот изображение моего стола enter image description here

1 Ответ

0 голосов
/ 27 июня 2019

Гораздо проще уменьшить массив до плоской структуры, чем создать шаблон, который отслеживает индекс.

const data = [
  { team: 'Team 1', score: 10 },
  { team: 'Team 2', score: 4 },
  { team: 'Team 3', score: -1 },
  { team: 'Team 4', score: -4 },
  { team: 'Team 5', score: 8 },
  { team: 'Team 6', score: 6 },
];

const mergeTeams = array => array.reduce((results, current, index) => {
  if (index % 2 === 0) {
    results.push(current);
  } else {
    const lastRow = results[results.length - 1];
    lastRow.team = `${lastRow.team} @ ${current.team}`;
    lastRow.awayScore = current.score;
  }
  return results;
}, []);

console.log(mergeTeams(data));
...