Angular 4-每个索引数组

Yuv*_*raj 0 arrays foreach angular

如何forEach在Angular4代码中标识循环的索引。

我需要根据条件在foreach内拼接记录。

angular.forEach(myObject => {
    if(!myObject.Name)
        myObject.splice(..., 1)
};
Run Code Online (Sandbox Code Playgroud)

如果其中的名称myObject为空,我想在这里删除对象。

HDJ*_*MAI 10

forEach 在此处记录:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

为了能够在此forEach循环内使用索引,可以通过以下方式添加索引:

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

@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
    name = 'Angular 6';

    myArray = [{name:"a"}, {name:""}, {name:"b"}, {name:"c"}];

    ngOnInit() {
        this.removeEmptyContent();
    }

    removeEmptyContent() {
        this.myArray.forEach( (myObject, index) => {
          if(!myObject.name){
              this.myArray.splice(index, 1);
          }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

工作链接演示:

https://stackblitz.com/edit/hello-angular-6-g1m1dz?file=src/app/app.component.ts