Javascript 如何在typescript中正确显示对象数组?

Javascript 如何在typescript中正确显示对象数组?,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个typescript中的对象数组: data: {number: number, name: string, department: string, city: string}[] = []; 我想将员工数据添加到数据对象中,并在表(ng2智能表)中显示它 getEmployees(val:any):无效{ 此._employeeService.getEmployees(val).subscribe((结果)=>{ this.employee=result.items; for(设i=

我有一个typescript中的对象数组:

data: {number: number, name: string, department: string, city: string}[] = [];
我想将员工数据添加到数据对象中,并在表(ng2智能表)中显示它

getEmployees(val:any):无效{
此._employeeService.getEmployees(val).subscribe((结果)=>{
this.employee=result.items;
for(设i=0;i
发生的情况是,它只打印数组的最后一个值。。。我想打印数组中的每个值。下面是一个正在发生的事情的例子:


提前感谢

您可以直接绑定到员工数组

<table>
  <thead>
    <tr>
      <th>Number</th>
      <th>Name</th>
      <th>Department<th>
      <th>City</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let e in employee">
      <td>{{e.number}}</td>
      <td>{{e.name}}</td>
      <td>{{e.departmentFk.name}}</td>
      <td>{{e.cityFk.name}}</td>
    </tr>
  </tbody>
</table>

但是,当您已经拥有所需格式的employee数组时,这似乎是多余的。您的版本只是尝试将一个数组复制到另一个数组,而不进行任何修改。

您可以轻松使用
forEach
以更方便的方式执行此任务:

getEmployees(val:any): void {
this._employeeService.getEmployees(val).subscribe((result) => {
    this.employee = result.items;
    this.employee.forEach((item) => {
        this.data.push({number : item.number,
                        name: item.name,
                        department: item.departmentFk.name,
                        city: item.cityFk.name});
    });
console.log(this.data);

正如其他人提到的,您需要将新对象推入数据变量

getEmployees(val:any): void {
    this._employeeService.getEmployees(val).subscribe((result) => {
        const employee = result.items;
        for (let i = 0; i < this.employee.length; i++) {
          this.data.push({
            'number': employee[i].number,
            'name': employee[i].name,
            'department': employee[i].departmentFk.name,
            'city': employee[i].cityFk.name,
          });
          this.source.append(this.data);
          this.source.refresh();
        }
    });
}

constructor() { this.source = new LocalDataSource(this.data); }
getEmployees(val:any):无效{
此._employeeService.getEmployees(val).subscribe((结果)=>{
const employee=result.items;
for(设i=0;i

您现在尝试的是在每个循环中更新数据的当前值,而不是在数组中添加新对象。

是否因为
this.data[“name”]
this.data.name
显示了错误?如果OP想要创建新的对象数组,Ivan的映射解决方案是最佳选择。是的,我同意。Map的工作速度比循环快。我的答案只是创建对象数组的另一种方法。我想看看map比forEach快多少。forEach大约慢了70%——我没想到会有那么大的不同!
data.push({
  number: this.employee[i].number,
  // etc...
})
getEmployees(val:any): void {
    this._employeeService.getEmployees(val).subscribe((result) => {
        const employee = result.items;
        for (let i = 0; i < this.employee.length; i++) {
          this.data.push({
            'number': employee[i].number,
            'name': employee[i].name,
            'department': employee[i].departmentFk.name,
            'city': employee[i].cityFk.name,
          });
          this.source.append(this.data);
          this.source.refresh();
        }
    });
}

constructor() { this.source = new LocalDataSource(this.data); }