Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多个数组行拆分为表头和表行(按角度排列)_Javascript_Arrays_Angular - Fatal编程技术网

Javascript 多个数组行拆分为表头和表行(按角度排列)

Javascript 多个数组行拆分为表头和表行(按角度排列),javascript,arrays,angular,Javascript,Arrays,Angular,我有一个具有以下结构的数组 0: "ID,NAME,NUMBER,DESCRIPTION" 1: "123,"PHILIP",0123456789,"OFFICE ADDRESS" 2: "456,"SARA",30345004698,"OFFICE SPACE"" 0: "ID、名称、编号、说明” 1:“菲利普123”,0123456789,“办公地址” 2:“456”,莎拉,30345004698,“办公空间” 我需要在表格中显示它。表格标题将是第0行,其余行将是表体行(tr)。请帮助我

我有一个具有以下结构的数组

0: "ID,NAME,NUMBER,DESCRIPTION" 1: "123,"PHILIP",0123456789,"OFFICE ADDRESS" 2: "456,"SARA",30345004698,"OFFICE SPACE"" 0: "ID、名称、编号、说明” 1:“菲利普123”,0123456789,“办公地址” 2:“456”,莎拉,30345004698,“办公空间” 我需要在表格中显示它。表格标题将是第0行,其余行将是表体行(tr)。请帮助我实现这一点

我可以显示表头,但表体显示不正确

表显示如下所示

请在下面找到我的代码

tableArray:任意;
tableArrayRows:任意[]=[];
表格标题:任意;
tableRows:任意[]=[];
//在新行拆分数组
this.tableArray=this.detailsArray.split(/\r?\n/);
//表格标题行
this.tableHeader=this.tableArray[0]。拆分(',');
//表体行
对于(var i=1;i

{{tableHeaders}}
{{tableRows}}

我认为问题在于

this.tableRows=this.tableArrayRows.split(',');

您缺少要拆分的tableArrayRows数组位置。如果问题是这样的:

this.tableRows=this.tableArrayRows[this.tableArrayRows.length-1].split(',');

应该修复它。

请参阅预期输出

在组件中

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnInit {
  tableArray: any;
  tableArrayRows: any[] = [];
  tableHeader: any;
  tableRows: any[] = [];
  detailsArray :any= `"ID,NAME,NUMBER,DESCRIPTION"
  "123,"PHILIP",0123456789,"OFFICE ADDRESS"
  "456,"SARA",30345004698,"OFFICE SPACE""`;

  ngOnInit() {

    // Splitting the array at new line
    this.detailsArray = this.detailsArray.replaceAll('"','');
    this.tableArray = this.detailsArray.split(/\r?\n/);
    // Table header row
    this.tableHeader = this.tableArray[0].split(',');
    // Table body row
    this.tableRows = this.tableArray.splice(1, this.tableArray.length);
    this.tableRows = this.tableRows.map(x => x.split(','));
  }
}
在HTML中

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th *ngFor="let tableHeaders of tableHeader">{{tableHeaders}}</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let tableRow of tableRows">
            <td *ngFor="let item of tableRow">{{ item }}</td>
        </tr>
    </tbody>
</table>

{{tableHeaders}}
{{item}}

不,这并没有解决问题。更改代码后,我得到了空行。谢谢Janardhan。但是数组结构与我的情况不同。请检查我帖子中的数组结构好吗?我做了以下更改,工作正常。{{cell}