使用Angular 4中的地图操作员进行对象分配



我需要使用数组将一个对象映射到另一个对象。我如何在Angular4中实现这一目标。目前,我正在使用foreach循环将项目推入数组。我需要使用地图。

     export interface GraphConfiguration  {
            seriesName: string;
            color: string;
        }
        export interface StressTestAnalysis extends GraphConfiguration {
            curlevel: number;
            decrease: number;
            increase: number;
            yaxis: number[];
            data: number[];
        }
        public results: Array<StressTestAnalysis> = [];
        private _stressResults: Array<StressTestAnalysis> = [];
         @Input() set stressResults(value: Array<StressTestAnalysis>) {
            this._stressResults = value;
            this.addSeries();
            let minY = Math.min(...this.yAxisSeries.map(el => Math.min(...el.yaxis)));
            let maxY = Math.max(...this.yAxisSeries.map(el => Math.max(...el.yaxis)));
    this.generateYAxisArray(minY, maxY);
         }
            this.results.forEach(element => {
                  if (element.data !== null)
                    this.chartSeries.push({ data: element.data, name: element.seriesName, color: element.color });
                   if (element.yaxis !== null)
                    this.yAxisSeries.push({ yaxis: element.yaxis });
                });
private addSeries() {
    if (this._stressResults === null) {
      return;
    }
 private generateYAxisArray(min: any, max: any) {
    let count = min;
    for (count = min; count <= max; count = count + 500000) {
      this.yAxisData.push(count);
    }
  }

我正在尝试实现以下内容。请注意,结果应包含图表和Yaxisseries。我正在考虑删除foreach循环,因为我认为这会导致重复。

 this.results =  this._stressResults.map((result: any) => {
   return;
    });

直方图组件代码

  <div *ngIf="!showTable" class="tab-pane base-strategy-chart fade show active" id="base-strategy-chart--nva" role="tabpanel"
          aria-labelledby="chart-tab">
          <div class="tb-container">
            <div class="tb-row d-flex flex-row">
              <div class="tb-cell col-12 pt-5">
               <splinechart [series]="chartSeries" [yaxisdata]="yAxisData">
               </splinechart>
                </div>
            </div>
          </div>

尝试这个。

    var numbers = [1, 4, 9];
    this.results =  this.numbers.map((result) => {
           console.log('Your result:', result);
           // Add your business logic to modify result
           new_result = result + 1
           return new_result;
    });
    console.log('Your final results array:', this.results);

最新更新