如何将2行数据合并为单行?

时间:2019-06-26 17:39:07

标签: javascript json angular html-table

我正在尝试将2 Line数据合并到每一行的单行中。例如1st row Green Bay Packers @ Chicago Bears行应包含+3.5000 and -3.5000数据,类似于Atlanta @ Minnesota行应包含+46.000 and +46.000。如何获得这样的输出?

x.component.ts代码

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代码

<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 :(得分:0)

将数组简化为平面结构比制作处理索引跟踪的模板要容易得多。

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));