Javascript 如何将两行数据合并为一行?

Javascript 如何将两行数据合并为一行?,javascript,json,angular,html-table,Javascript,Json,Angular,Html Table,我正在尝试为每行将2个行数据合并到一行中。例如,第1行是 Green Bay Packers@Chicago Bears行应包含+3.5000和-3.5000数据,同样,对于Atlanta@Minnesota行,应包含+46.000和+46.000。我怎样才能获得这样的产出 x、 component.ts代码 import {Component} from '@angular/core'; import {HttpClient} from '@angular/common/http' impor

我正在尝试为每行将2个
数据合并到一行中。例如,第1行是
Green Bay Packers@Chicago Bears
行应包含
+3.5000和-3.5000
数据,同样,对于
Atlanta@Minnesota
行,应包含
+46.000和+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);
    }
  }
}

从'@angular/core'导入{Component};
从“@angular/common/http”导入{HttpClient}
从“rxjs”导入{forkJoin};
从“rxjs/operators”导入{map};
@组成部分({
选择器:“应用程序mlb api”,
templateUrl:“./mlb api.component.html”,
样式URL:['./mlbapi.component.css']
})
导出类MlbApiComponent{
//allhomeTeamName;
allawayTeamName;
allline;
全名;
全部:数组=[];
一线游戏:阵列;
奇形怪状的美国人:数组;
构造函数(私有http:HttpClient){}
恩戈尼尼特(){
const character=this.http.get('https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json管道(映射((re:any)=>re.events));
const characterHomeworld=this.http.get('https://www.fantasylabs.com/api/sportevents/3/2019_06_17');
this.firstLinePerGame=新数组();
this.oddsAmericans=新数组();
forkJoin([character,characterHomeworld])。订阅([draftkingsResp,FantasyLabResp])=>{
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.outcouts).flat().map(o=>o.line);
this.allline=this.allline.filter(l=>!!l);
这个.createAllArray();
draftkingsResp.forEach(r=>{
if(r.offers&&r.offers.length){
if(r.offers.length==3){
const firstGame=r.offers[0]。结果[0];
这个.firstLinePerGame.push(firstGame.line);
//const secondGame=r.offers[2]。结果[0];
//这个.firstLinePerGame.push(secondGame.line);
this.oddsAmericans.push(r.offers[1].outlets[0].oddsAmerican)
this.oddsAmericans.push(r.offers[1].outlets[1].oddsAmerican)
}else if(r.offers.length==1){
const firstGame=r.offers[0]。结果[0];
这个.firstLinePerGame.push(firstGame.line);
}else if(r.offers.length==2){
console.log('*********************')
}
}
})      
log(this.firstLinePerGame.filter(l=>l));
console.log(this.oddsAmericans);
});
}
createAllArray():void{
for(设i=0;i
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>


Name

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

const数据=[
{球队:'1队',得分:10},
{球队:'第二队',得分:4},
{球队:'3队',得分:-1},
{球队:'4队',得分:-4},
{球队:'5队',得分:8},
{球队:'6队',得分:6},
];
const mergeTeams=array=>array.reduce((结果、当前、索引)=>{
如果(索引%2==0){
结果:推送(电流);
}否则{
const lastRow=results[results.length-1];
lastRow.team=`${lastRow.team}@${current.team}`;
lastRow.awayScore=当前的.score;
}
返回结果;
}, []);

console.log(合并团队(数据))将数组简化为平面结构要比制作处理索引跟踪的模板容易得多

const数据=[
{球队:'1队',得分:10},
{球队:'第二队',得分:4},
{球队:'3队',得分:-1},
{球队:'4队',得分:-4},
{球队:'5队',得分:8},
{球队:'6队',得分:6},
];
const mergeTeams=array=>array.reduce((结果、当前、索引)=>{
如果(索引%2==0){
结果:推送(电流);
}否则{
const lastRow=results[results.length-1];
lastRow.team=`${lastRow.team}@${current.team}`;
lastRow.awayScore=当前的.score;
}
返回结果;
}, []);

console.log(合并团队(数据))我正在处理多个API调用,我已经列出了两个API,其中一个是,您能帮助回答这方面的问题吗?谢谢,我正在处理多个API调用,我已经列出了两个API,其中一个是,你能帮助回答这方面的问题吗?谢谢