【问题标题】:I can't do a foreach of an Object我不能做一个对象的 foreach
【发布时间】:2019-11-22 14:21:46
【问题描述】:

所以我试图为每个对象制作一个,但我做不到,我真的不知道为什么。

所以我有这个:

export class ProcessesModel {
  processName: string
  status: string

我将这个类中的变量作为一个数组:

processStatus: ProcessesModel[] = [];

然后我通过推送将值发送到变量,如下所示:

getProcessState(refDate: string) {

this.processStatus = new Array<ProcessesModel>();

for (const pClass of Array.from(this.processClasses.keys())) {
  this.dasboardService.getProcessSate(pClass, refDate)
  .then(
    (state) => {
      switch (state) {
        case 'Successful':
          this.processClasses.set(pClass, 'input successful');
          this.processStatus.push({processName: pClass, status: 'input successful'});
          break;
        case 'Executing':
          this.processClasses.set(pClass, 'input inprogress');
          this.processStatus.push({processName: pClass, status: 'input inprogress'});
          break;
        case 'Error':
          this.processClasses.set(pClass, 'input error');
          this.processStatus.push({processName: pClass, status: 'input error'});
          break;
        case 'Not Executed':
          this.processClasses.set(pClass, 'input');
          this.processStatus.push({processName: pClass, status: 'input'});
          break;
        default:
          this.processClasses.set(pClass, 'input');
          this.processStatus.push({processName: pClass, status: 'input'});
      }
    },
    (error) => {
      this.processClasses.set(pClass, 'input error');
      this.processStatus.push({processName: pClass, status: 'input error'});
    });

  }

然后我用 eventEmitter 发送这个变量,当我订阅那个变量时,它不允许我做一个 foreach 循环。它根本没有做任何事情,甚至没有错误。

当我打印一个普通对象与这个对象时,它会像这样打印:

我可以从第一个对象中进行 foreach,但不能使用第二个对象...

编辑:当我收到所有数据时,我保存到一个新变量中:

processes: Array<any> = [];

这是我的订阅:

this.captoolsComp.processes.subscribe((processes) => {
  this.processes = processes;

  processes.forEach(process => {
    this.setProcState(process.processName, process.status);
  });

【问题讨论】:

  • 你能发布你的服务吗?
  • 你能发布你试图迭代的数据(文本而不是图像)和你编写的试图迭代的 foreach 函数吗?

标签: javascript arrays angular typescript foreach


【解决方案1】:

我的 CONTENT 是数组,它的对象为

{ status: 1}
{ status: 0}
{ status: 1}

所以我想迭代每个对象,然后我想修改元素,使其以这种方式工作

rowData : any;
x: any;
 this.rowData = response['CONTENT'];
             console.log( this.rowData);
             this.rowData.forEach( (element) => {
      this.x = element.status ;
             if ( this.x = 1) {

             element.status = 'Open';

                }
              else {
             element.status = 'Close';
               }

          });
          console.log( this.rowData);

所以现在我在最终的 console.log 中得到了修改后的 json

【讨论】:

  • 这对 OP 有何帮助?
猜你喜欢
  • 2018-01-03
  • 2017-06-02
  • 2022-01-25
  • 2020-07-20
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 2021-06-13
  • 2016-08-28
相关资源
最近更新 更多